实体类和Dao类:
package com.fast.cn.ipaddress.dao.domain; import com.fast.cn.es.annotations.Document; import com.fast.cn.es.annotations.Field; import com.fast.cn.es.annotations.ID; import com.fast.cn.es.data.FieldIndex; import com.fast.cn.es.data.FieldType; import javax.persistence.Column; import javax.persistence.Entity; /** * Created with IntelliJ IDEA. * Description: * UserInfo: wangpeng * Date: 2018-03-09 * Time: 11:19 */ @Document(index = "area", type = "china", replicas = 1, shards = 5) @Entity(name = "ipaddress") public class IpaddressResult { @ID @Column(name = "id") private String id; @Column(name = "minip") @Field(type = FieldType.Double, index = FieldIndex.not_analyzed) private double minip; @Column(name = "maxip") @Field(type = FieldType.Double, index = FieldIndex.not_analyzed) private double maxip; /* @Column(name = "contient") @Field(type = FieldType.text, index = FieldIndex.analyzed) private String contient;*/ @Column(name = "areacode") @Field(type = FieldType.text, index = FieldIndex.analyzed) private String areacode; @Column(name = "country") @Field(type = FieldType.text, index = FieldIndex.analyzed) private String country; @Column(name = "multiarea") @Field(type = FieldType.text, index = FieldIndex.analyzed) private String multiarea; @Column(name = "user") @Field(type = FieldType.text, index = FieldIndex.analyzed) private String user; public String getId() { return id; } public void setId(String id) { this.id = id; } public double getMinip() { return minip; } public void setMinip(double minip) { this.minip = minip; } public double getMaxip() { return maxip; } public void setMaxip(double maxip) { this.maxip = maxip; } /* public String getContient() { return contient; } public void setContient(String contient) { this.contient = contient; } */ public String getAreacode() { return areacode; } public void setAreacode(String areacode) { this.areacode = areacode; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getMultiarea() { return multiarea; } public void setMultiarea(String multiarea) { this.multiarea = multiarea; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } }
实现接口:
package com.fast.cn.ipaddress.service; import com.fast.cn.es.model.NmpScanResult; import com.fast.cn.ipaddress.dao.domain.IpaddressResult; import java.util.Map; public interface IpaddressService { IpaddressResult buildResultGrid(String nmp); }
实现类:
package com.fast.cn.ipaddress.service.impl; import com.fast.cn.es.Beans; import com.fast.cn.es.accessor.IAccessor; import com.fast.cn.ipaddress.dao.domain.IpaddressResult; import com.fast.cn.ipaddress.service.IpaddressService; import com.fast.cn.util.JsonUtils; import org.apache.log4j.Logger; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; import static com.fast.cn.util.CommonUtil.ipToLong; @Service("ipaddressService") public class IpaddressServiceImpl implements IpaddressService { public static Logger log = Logger.getLogger(IpaddressServiceImpl.class); private static IAccessor accessor = Beans.getAccessor(); @Override public IpaddressResult buildResultGrid(String nmp) { // Map<String, Object> updateMap = new HashMap<>(); long ip = ipToLong(nmp); //设置查询体 QueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.rangeQuery("maxip").gte(ip)) .must(QueryBuilders.rangeQuery("minip").lte(ip)); //查询ES库 List<IpaddressResult> ipaddressResults = accessor.search(IpaddressResult.class, queryBuilder); if (ipaddressResults.size() != 0) { IpaddressResult ipaddressResult = ipaddressResults.get(0); /* String multiarea = ipaddressResult.getMultiarea(); List<Map> jsonList = JsonUtils.jsonToList(multiarea, Map.class);*/ return ipaddressResult; } return null; } }
controller类:
package com.fast.cn.ipaddress.web; import com.fast.cn.ipaddress.dao.domain.IpaddressResult; import com.fast.cn.ipaddress.service.IpaddressService; import com.fast.cn.ipaddress.service.JavadaoruService; import com.fast.cn.util.DataResult; import com.fast.cn.util.JsonUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.*; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller @RequestMapping(value = "/ipaddress") public class IpaddressController { @Autowired private IpaddressService ipaddressService; @Autowired private JavadaoruService javadaoruService; @RequestMapping(value = "/buildResultGrid") @ResponseBody public Object buildResultGrid( String nmp) { /* Map<String, Object> model = new HashMap<>();*/ Map<String, Object> model = new HashMap<>(); IpaddressResult ipaddressResult = ipaddressService.buildResultGrid( nmp ); if(ipaddressResult!=null){ model.put("ipaddressResult", ipaddressResult); List<Map> jsonList = JsonUtils.jsonToList(ipaddressResult.getMultiarea(), Map.class); // Map map = jsonList.get(0); if(jsonList.size()>0){ Map m1 =jsonList.get(0); model.put("w", m1.get("w")); model.put("j", m1.get("j")); model.put("p", m1.get("p")); model.put("c", m1.get("c")); model.put("d", m1.get("d")); } // model.put("map", map); // Map map1 = jsonList.get(1); // //* model.addAttribute("map2", map1);*//* // model.put("map1", map1); // Map map2 = jsonList.get(2); // *//* model.addAttribute("map2", map2);*//* // model.put("map2", map2); // Map map3 = jsonList.get(3); // *//* model.addAttribute("map4", map3);*//* // model.put("map3", map3); // Map map4 = jsonList.get(4); // *//* model.addAttribute("map5", map4);*//* // model.put("map4", map4); return model; } return null; } @RequestMapping(value = "/goipaddress") public ModelAndView result() { ModelAndView model = new ModelAndView(); model.setViewName("ipaddress/ipaddressResult"); return model; } @RequestMapping("/javapoi") @ResponseBody public DataResult add(@RequestParam("file") MultipartFile file) throws IOException { List<IpaddressResult> sss=javadaoruService.javaPoi(file.getInputStream()); return DataResult.ok(sss); } @RequestMapping("/downLoad") public void downLoad(HttpServletRequest req,HttpServletResponse res){ //获取要下载的模板名称 String fileName = req.getParameter("base.xls"); //设置要下载的文件的名称 res.setHeader("Content-disposition","attachment;fileName="+"base.xls"); //通知客服文件的MIME类型 res.setContentType("application/vnd.ms-excel;charset=UTF-8"); //获取文件的路径 String filePath = req.getSession().getServletContext().getRealPath("/WEB-INF/file")+"/"+"base.xls"; filePath = filePath.replace("\\", "/"); InputStream input = null; OutputStream out =null; try { input = new FileInputStream(filePath); out =res.getOutputStream(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } byte[] b = new byte[2048]; int len; try { while((len=input.read(b))!=-1){ out.write(b,0,len); } } catch (IOException e) { e.printStackTrace(); } try { input.close(); } catch (IOException e) { e.printStackTrace(); } } }
页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> #allmap {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";font-size:14px;} #l-map{height:500px;width:100%;} #r-result{width:100%;} </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=pb42PdmPlaYZ0NlOuGPgV63jMki9Ga2G"></script> <%--基础引用--%> <jsp:include page="../common/commonQuote.jsp"/> <script type="text/javascript"> function KeyDown() { if (event.keyCode == 13) { // alert("324124") event.returnValue=false; event.cancel = true; // Form1.btnsubmit.click(); getIdAddress(); } } function uploaderFile(e){ var file=e.target.files[0] console.log(file) var from_ = new FormData(); from_.append('file', file); //这里写上传的ajax $.ajax({ type:"post", url: basePath + "/ipaddress/javapoi", data: from_, dataType: "json", cache: false,//上传文件无需缓存 processData: false,//用于对data参数进行序列化处理 这里必须false contentType: false, //必须 success:function(result){ alert('上传成功') } }) } function getIdAddress(){ var nmp= $("#nmp").val(); $.ajax({ type: "post", url: basePath + "/ipaddress/buildResultGrid", data: {nmp:nmp}, dataType: "json", error: function () { alert("请检查IP地址参数!!!"); }, success: function (data) { console.log("data",data); $("#country").html(data.ipaddressResult.country); $("#city").html(data.c); $("#jingdu").html(data.j); $("#weidu").html(data.w); // 百度地图API功能 var sContent ="所查询到的地点,在以下这个位置。"; var map = new BMap.Map("l-map"); var point = new BMap.Point(data.j, data.w); map.centerAndZoom(point, 15); var infoWindow = new BMap.InfoWindow(sContent); // 创建信息窗口对象 // map.openInfoWindow(infoWindow,point); //开启信息窗口 map.enableScrollWheelZoom();//启动鼠标滚轮缩放地图 marker = new BMap.Marker(point); map.addOverlay(marker); map.panTo(point); } }); } function downloadTemplate() { window.location.href=basePath + "/ipaddress/downLoad" } </script> <title>IP地址库</title> </head> <body ><!--herder--> <div class="header"> <div class="content-width clear"> <div class="header-left"><img src="/assets/images/logo.png" alt="logo" width="73" height="52" > <a href="#">首页</a><a href="#">网络指纹识别</a><a href="#">网络指纹检索</a><a href="#">漏洞验证</a><a href="#">漏洞上报</a><a href="#">IP地址库</a><a href="#">DNS库</a><a href="#">规则库</a></div> <div class="header-right"> <form class="header-search"> <input type="text" id="searchInput" value=""> <img class="header-icon" src="/assets/images/search.png" alt=""> </form> <div class="header-login"> <div><img src="/assets/images/member.png" alt="more"></div> <div class="header-links"><a href="#"><i class="fa fa-list-alt" style="margin-right:10px"></i>个人资料</a><a href=""><i class="fa fa-unlock-alt" style="margin-right:10px"></i>更改密码</a><a href=""><i class="fa fa-sign-out" style="margin-right:10px"></i>退出登录</a></div> </div> </div> </div> </div> <!--herder--> <!--content--> <div class="currency"> <div class="content-width currency-content clearfix"> <div class="currency-left"> <div class="result-sslxfont">IP地址库</div> <div class="result-sslxfoot"></div> <!--country--> <div class="result-jiange"> <div class="kjts-result-year">中国</div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">天津</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">414156</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">北京</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">481101</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">上海</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">14524</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">哈尔滨</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">8210</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">深圳</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">14524</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">重庆</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">8210</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">白山</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">14524</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">青岛</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">8210</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">沈阳</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">14524</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin"><a href="#">贵州</a> </span> <span class="databox-text pull-right no-margin label label-huiheiberry">8210</span> </div> </div> <!--country--> <!-- country--> <div class="result-jiange"> <div class="kjts-result-year">国家</div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin">日本</span> <span class="databox-text pull-right no-margin label label-huiheiberry">16</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin">美国</span> <span class="databox-text pull-right no-margin label label-huiheiberry">80</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin">韩国</span> <span class="databox-text pull-right no-margin label label-huiheiberry">8</span> </div> <div class="databox-row padding-10 margin-10"> <span class="databox-text pull-left no-margin">加拿大</span> <span class="databox-text pull-right no-margin label label-huiheiberry">8210</span> </div> </div> <!-- country--> </div> <!--right--> <div class="currency-right"> <!--tongji biaoti--> <div class="kjgz-lodong-title"> <div class="kjgz-lodong-titlt-bt"><img src="/assets/images/loudong-tj.png" width="28" height="24" style="margin-right:10px">IP数据导入</div> </div> <!--tongji biaoti--> <!--tab1content--> <div class="kjgz-font16 margin-bottom-20" > <form class="form-inline" role="form"> <div class="form-group margin-left-30"> <span class="text">文件格式要求:</span> <a class="btn btn-default kjgz-blue" href="javascript:void(0);" onclick="downloadTemplate();" ><i class="glyphicon glyphicon-save margin-right-10"></i>模板下载</a> <%-- <input type="button" class="btn_default" onclick="downloadTemplate();" value="下载模板"/>--%> </div> <form id="uploadForm" enctype="multipart/form-data" method="post"> <div class="form-group has-error margin-left-30"> <span class="text">IP地址库导入:</span> <input style="display: none" onchange="uploaderFile(event)" type="file" name="file" id="file" class="form-control input-sm" placeholder=""> <%--<input type="button" id="upload" value="上传" onclick="$('#file').click()"/>--%> <a class="btn btn-default kjgz-blue" href="javascript:void(0);" id="upload" onclick="$('#file').click()"><i class="glyphicon glyphicon-save margin-right-10"></i>上传文件</a> </div> <form/> <%-- <a class="btn btn-kj-orange btn-sm" href="javascript:void(0);" style=" font-size:14px"><i class="glyphicon glyphicon-folder-open"></i>点击上传附件</a> --%> </form> </div> <!--tab1content--> <!--tongji biaoti--> <div class="kjgz-lodong-title"> <div class="kjgz-lodong-titlt-bt"><img src="/assets/images/loudong-ss.png" width="29" height="24" style="margin-right:10px">IP查询</div> </div> <!--tongji biaoti--> <!-- search--> <div class="" > <form class="form-inline" role="form" method="post" name="Form1" > <div class="form-group margin-left-30" > <span class="text" style="font-size:14px;">IP地址:</span> <input type="text" id="nmp" name="nmp" class="form-control input-sm" size="80" placeholder="" onkeydown="KeyDown();"> </div> <div id="submit" name="btnsubmit" onclick="getIdAddress()" class="btn btn-blue btn-sm margin-left-30"><i class="fa fa-search" style="font-size:20px"></i>搜 索</div> </form> </div> <!-- search--> <!--tab1content--> <div class="margin-20"> <ul class="margin-left-10 kjgz-font14"> <li class="kjgz-ld-xq-titlexq"><span class="kjgz-ld-xq-option">国家:</span><span class="kjgz-ld-xq-value margin-right-40" id="country" name="country" ></span></li> <li class="kjgz-ld-xq-titlexq"><span class="kjgz-ld-xq-option">城市:</span><span class="kjgz-ld-xq-value margin-right-40" id="city" name="city"></span></li> <li class="kjgz-ld-xq-titlexq"><span class="kjgz-ld-xq-option">经度:</span><span class="kjgz-ld-xq-value margin-right-40" id="jingdu" name="jingdu"</span></li> <li class="kjgz-ld-xq-titlexq"><span class="kjgz-ld-xq-option">纬度:</span><span class="kjgz-ld-xq-value margin-right-40" id="weidu" name="weidu"></span></li> </ul> </div> <div id="l-map"></div> <div id="r-result"></div> </div> <!--tab1content--> <div class="clear"></div> </div> <!-- righ--> </div> </div> <!--content--> <!--foot--> <div class="footer"> <div class="footer-copyright content-width">Copyright Ⓒ 天津市兴先道科技有限公司 2018.All Rights Reserved 津ICP备案审核中</div> </div> <!--foot--> </body> </html> <script type="text/javascript"> // 百度地图API功能 var sContent ="天安门坐落在中国北京市中心,故宫的南侧,与天安门广场隔长安街相望,是清朝皇城的大门..."; var map = new BMap.Map("l-map"); var point = new BMap.Point(117.15, 39.13); map.centerAndZoom(point, 15); var infoWindow = new BMap.InfoWindow(sContent); // 创建信息窗口对象 // map.openInfoWindow(infoWindow,point); //开启信息窗口 map.enableScrollWheelZoom();//启动鼠标滚轮缩放地图 // document.getElementById("r-result").innerHTML = "信息窗口的内容是:<br />" + infoWindow.getContent(); marker = new BMap.Marker(point); map.addOverlay(marker); map.panTo(point); </script>