java web判断服务器是否是本机

你。 提交于 2019-12-05 13:43:31

1,如何获取浏览器的ip

Java代码   收藏代码
  1. /*** 
  2.      * 获取客户端ip地址(可以穿透代理) 
  3.      * @param request 
  4.      * @return  
  5.      */  
  6.     public static String getClientIpAddr(HttpServletRequest request) {    
  7.         String ip = request.getHeader("X-Forwarded-For");    
  8.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  9.             ip = request.getHeader("Proxy-Client-IP");    
  10.         }    
  11.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  12.             ip = request.getHeader("WL-Proxy-Client-IP");    
  13.         }    
  14.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  15.             ip = request.getHeader("HTTP_X_FORWARDED_FOR");    
  16.         }    
  17.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  18.             ip = request.getHeader("HTTP_X_FORWARDED");    
  19.         }    
  20.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  21.             ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");    
  22.         }    
  23.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  24.             ip = request.getHeader("HTTP_CLIENT_IP");    
  25.         }    
  26.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  27.             ip = request.getHeader("HTTP_FORWARDED_FOR");    
  28.         }    
  29.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  30.             ip = request.getHeader("HTTP_FORWARDED");    
  31.         }    
  32.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  33.             ip = request.getHeader("HTTP_VIA");    
  34.         }    
  35.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  36.             ip = request.getHeader("REMOTE_ADDR");    
  37.         }    
  38.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
  39.             ip = request.getRemoteAddr();    
  40.         }    
  41.         return ip;    
  42.     }  
  43.     public static String getIpAddr(HttpServletRequest request) {  
  44.         String ip = request.getHeader("X-Real-IP");  
  45.         if (null != ip && !"".equals(ip.trim())  
  46.                 && !"unknown".equalsIgnoreCase(ip)) {  
  47.             return ip;  
  48.         }  
  49.         ip = request.getHeader("X-Forwarded-For");  
  50.         if (null != ip && !"".equals(ip.trim())  
  51.                 && !"unknown".equalsIgnoreCase(ip)) {  
  52.             // get first ip from proxy ip  
  53.             int index = ip.indexOf(',');  
  54.             if (index != -1) {  
  55.                 return ip.substring(0, index);  
  56.             } else {  
  57.                 return ip;  
  58.             }  
  59.         }  
  60.         return request.getRemoteAddr();  
  61.     }  

 

 

2,如何判断服务器是否是本机

Java代码   收藏代码
  1. /*** 
  2.      * 服务器是否是本机 
  3.      * @param request 
  4.      * @return  
  5.      */  
  6.     public static boolean isLocalIp(HttpServletRequest request){  
  7.         String ip=WebServletUtil.getClientIpAddr(request);  
  8.         return ip.equals("127.0.0.1")||ip.equals("localhost")||ip.equals("0:0:0:0:0:0:0:1");  
  9.     }  

 3,应用:

Java代码   收藏代码
  1. /*** 
  2.      * favicon.ico  
  3.      * @throws IOException  
  4.      */  
  5.     @RequestMapping(value = "/favicon.ico")  
  6.     public ResponseEntity<byte[]> faviconIco(HttpServletRequest request) throws IOException {  
  7.         HttpHeaders headers = new HttpHeaders();  
  8.         headers.setContentType(MediaType.IMAGE_PNG);  
  9.         String faviconIcoName="sms-4.ico";  
  10.         headers.set(Constant2.CONTENT_DISPOSITION,WebServletUtil.getContentDisposition(true, faviconIcoName));  
  11.         ///home/whuang/software/apache-tomcat-7.0.53/webapps/ROOT/  
  12.         String webappPath=null;  
  13.         if(WebServletUtil.isLocalIp(request)){//服务器在本机(访问ip为127或localhost)  
  14.             webappPath=WebServletUtil.getRealPath(request);  
  15.         }else{  
  16.             webappPath=DictionaryParam.get(Constant2.DICTIONARY_GROUP_GLOBAL_SETTING, "WEB-INF_LOC");  
  17.         }  
  18.         return new ResponseEntity<byte[]>(FileUtils.getBytes4File(  
  19.                 webappPath  
  20.                 +"WEB-INF/static/img/"+faviconIcoName),  
  21.                                           headers, HttpStatus.CREATED);  
  22.   
  23.     }  

 

4,如何获取服务器部署的本地路径

Java代码   收藏代码
  1. public static String getRealPath(HttpServletRequest request) {  
  2.         return getRealPath(request,  "\\");  
  3.     }  
  4.     /** 
  5.      * 获取相对地址的绝对地址 
  6.      *  
  7.      * @param request 
  8.      * @param relativePath 
  9.      * @return 
  10.      */  
  11.     public static String getRealPath(HttpServletRequest request, String relativePath) {  
  12.         return request.getSession().getServletContext().getRealPath(relativePath);  
  13.     }  

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!