1,如何获取浏览器的ip
- /***
- * 获取客户端ip地址(可以穿透代理)
- * @param request
- * @return
- */
- public static String getClientIpAddr(HttpServletRequest request) {
- String 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.getHeader("HTTP_X_FORWARDED_FOR");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_X_FORWARDED");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_CLIENT_IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_FORWARDED_FOR");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_FORWARDED");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_VIA");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("REMOTE_ADDR");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getRemoteAddr();
- }
- return ip;
- }
- public static String getIpAddr(HttpServletRequest request) {
- String ip = request.getHeader("X-Real-IP");
- if (null != ip && !"".equals(ip.trim())
- && !"unknown".equalsIgnoreCase(ip)) {
- return ip;
- }
- ip = request.getHeader("X-Forwarded-For");
- if (null != ip && !"".equals(ip.trim())
- && !"unknown".equalsIgnoreCase(ip)) {
- // get first ip from proxy ip
- int index = ip.indexOf(',');
- if (index != -1) {
- return ip.substring(0, index);
- } else {
- return ip;
- }
- }
- return request.getRemoteAddr();
- }
2,如何判断服务器是否是本机
- /***
- * 服务器是否是本机
- * @param request
- * @return
- */
- public static boolean isLocalIp(HttpServletRequest request){
- String ip=WebServletUtil.getClientIpAddr(request);
- return ip.equals("127.0.0.1")||ip.equals("localhost")||ip.equals("0:0:0:0:0:0:0:1");
- }
3,应用:
- /***
- * favicon.ico
- * @throws IOException
- */
- @RequestMapping(value = "/favicon.ico")
- public ResponseEntity<byte[]> faviconIco(HttpServletRequest request) throws IOException {
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.IMAGE_PNG);
- String faviconIcoName="sms-4.ico";
- headers.set(Constant2.CONTENT_DISPOSITION,WebServletUtil.getContentDisposition(true, faviconIcoName));
- ///home/whuang/software/apache-tomcat-7.0.53/webapps/ROOT/
- String webappPath=null;
- if(WebServletUtil.isLocalIp(request)){//服务器在本机(访问ip为127或localhost)
- webappPath=WebServletUtil.getRealPath(request);
- }else{
- webappPath=DictionaryParam.get(Constant2.DICTIONARY_GROUP_GLOBAL_SETTING, "WEB-INF_LOC");
- }
- return new ResponseEntity<byte[]>(FileUtils.getBytes4File(
- webappPath
- +"WEB-INF/static/img/"+faviconIcoName),
- headers, HttpStatus.CREATED);
- }
4,如何获取服务器部署的本地路径
- public static String getRealPath(HttpServletRequest request) {
- return getRealPath(request, "\\");
- }
- /**
- * 获取相对地址的绝对地址
- *
- * @param request
- * @param relativePath
- * @return
- */
- public static String getRealPath(HttpServletRequest request, String relativePath) {
- return request.getSession().getServletContext().getRealPath(relativePath);
- }