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

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.     }  

 

转载于:https://my.oschina.net/pvpCC9IFwqz4/blog/533839

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值