java web判斷服務器是不是本機

1,如何獲取瀏覽器的ip web

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,如何判斷服務器是不是本機 apache

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,如何獲取服務器部署的本地路徑 tomcat

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.     }  
相關文章
相關標籤/搜索