JSP 웹 페이지 에서 사용자 IP 주소 정보 수집 - 취 합 데이터 IP 주소 데이터 인터페이스 사용
최근 에 프로젝트 문제 가 발생 했 는데 사용자 의 지역 정 보 를 수집 하여 사용자 의 지역 위 도 를 분석 해 야 한다.인터넷 에서 자 료 를 찾 았 는데 마침 취 합 데이터 API 의 IP 인터페이스 (http://www.juhe.cn/docs/api/id/1) 제 요구 에 딱 맞 아서 기록 해서 공유 해 드 리 겠 습 니 다.
JSP 웹 페이지 를 통 해 사용자 의 IP 주 소 를 가 져 옵 니 다:
1, BaseDAO 초기 화 및 변수 에 대한 설명
BaseDAO dao =(BaseDAOImpl)WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()).getBean("dao");
String ip="";//IP
double ipvalue = 0;
String fromUrl="";// URL
String toUrl="";// URL
String address="";//IP
IPLocalAddress ipLocationAddress = new IPLocalAddress();
2, IP 주소 가 져 오기
if(request.getRemoteAddr()!=null){
ip=request.getRemoteAddr();// IP
ip =request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 ||"unknown".equalsIgnoreCase(ip)) {
ip =request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 ||"unknown".equalsIgnoreCase(ip)) {
ip =request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 ||"unknown".equalsIgnoreCase(ip)) {
ip =request.getRemoteAddr();
}
System.out.println(ip);
// IP
//String[] tmp =ip.split("\\.");
//ipvalue =Double.parseDouble(tmp[0])*256*256*256+Double.parseDouble(tmp[1])*256*256+Double.parseDouble(tmp[2])*256+Double.parseDouble(tmp[3]);
}
if(request.getParameter("fromUrl")!=null&& request.getParameter("fromUrl").length()>0){
fromUrl=request.getParameter("fromUrl");// URL
}else{
fromUrl=" ";
}
if(request.getParameter("toUrl")!=null &&request.getParameter("toUrl").length()>0){
toUrl=request.getParameter("toUrl");// URL
}
if(ip.startsWith("192")||ip.startsWith("127")){
address=" ";
}else{
if(ip.equals("0:0:0:0:0:0:0:1%0") == false) {
3. 취 합 데 이 터 는 IP 를 통 해 주소 인 터 페 이 스 를 조회 합 니 다. (취 합 데이터 IP 인터페이스 주소)http://www.juhe.cn/docs/api/id/1
Map<String,String> params = new LinkedHashMap<String, String>();
params.put("key","4b62068c1fc6758c174bcc21e321cb6f");
params.put("ip", ip);
String url ="http://apis.juhe.cn/ip/ip2addr";
try {
ipLocationAddress = HttpJsonAdapter.getInstance().get( url,
params,
IPLocalAddress.class);
if(ipLocationAddress.getResultcode().equals("200")) {
address = ipLocationAddress.getResult().getArea() +"," + ipLocationAddress.getResult().getLocation();
}else {
address = "juhe data Exception";
}
//}}
} catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}else {
address ="0:0:0:0:0:0:0:1%0";
}
}
4, 데이터베이스 에 접근 기록 저장
Traffic traffic = new Traffic();
traffic.setIp(ip);
traffic.setSourceUrl(fromUrl);
traffic.setTargetUrl(toUrl);
traffic.setArea(address);
traffic.setVisitDate(new java.util.Date());
dao.saveOrUpdate(traffic);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JSP| EL (Experession Language)텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.