All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sylphlike.framework.utils.general.IPUtils Maven / Gradle / Ivy

The newest version!
package com.github.sylphlike.framework.utils.general;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Enumeration;


/**
 * IP地址工具类
 * 

time 10/09/2020 18:19 星期四 (dd/MM/YYYY HH:mm) *

email [email protected] * @author Gopal.pan * @version 1.0.0 */ public class IPUtils { private static final Logger LOGGER = LoggerFactory.getLogger(IPUtils.class); /** 获取IP地址区域服务(淘宝提供) */ private static final String IP_SERVER_ADDRESS = "http://ip.taobao.com/service/getIpInfo.php"; /** * 获取用户真实IP地址 *

不使用request.getRemoteAddr()的原因是有可能用户使用了代理软件方式避免真实IP地址, 如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值 *

time 11:02 2021/1/5 (HH:mm yyyy/MM/dd) *

email [email protected] * @param request request对象 * @return java.lang.String * @author Gopal.pan */ public static String clientRealIP(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { // 多次反向代理后会有多个ip值,第一个ip才是真实ip if( ip.contains(",")){ ip = ip.split(",")[0]; } } 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_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("X-Real-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } /** * 获取获取服务器IP地址 多网卡版 *

time 10:28 2021/1/5 (HH:mm yyyy/MM/dd) *

email [email protected] * @return java.lang.String * @author Gopal.pan */ public static String serverIPAddrMoreNetWork() { try { //一个主机有多个网络接口 Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface netInterface = netInterfaces.nextElement(); //每个网络接口,都会有多个"网络地址",比如一定会有loopback地址,会有siteLocal地址等.以及IPV4或者IPV6 . Enumeration addresses = netInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); //get only :172.*,192.*,10.* if (address.isSiteLocalAddress() && !address.isLoopbackAddress()) { return address.getHostAddress(); } } } } catch (SocketException e) { LOGGER.error("【framework-utils】获取服务器ID地址异常",e); } return null; } /** * 获取服务器IP地址 *

time 11:02 2021/1/5 (HH:mm yyyy/MM/dd) *

email [email protected] * @return java.lang.String * @author Gopal.pan */ public static String serverIPAddrSingleNetwork(){ try { InetAddress address = InetAddress.getLocalHost(); return address.getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } return null; } private static String getServerResult(String ipAddress) { HttpURLConnection connection = null; try { URL url = new URL(IP_SERVER_ADDRESS); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setReadTimeout(3000); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(ipAddress); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); return buffer.toString(); } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return null; } private static String decodeUnicode(String unicodeString){ char aChar; int len = unicodeString.length(); StringBuffer buffer = new StringBuffer(len); for (int i = 0; i < len;) { aChar = unicodeString.charAt(i++); if(aChar == '\\'){ aChar = unicodeString.charAt(i++); if(aChar == 'u'){ int val = 0; for(int j = 0; j < 4; j++){ aChar = unicodeString.charAt(i++); switch (aChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': val = (val << 4) + aChar - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': val = (val << 4) + 10 + aChar - 'a'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': val = (val << 4) + 10 + aChar - 'A'; break; default: throw new IllegalArgumentException("Malformed encoding"); } } buffer.append((char) val); }else{ if(aChar == 't'){ aChar = '\t'; } if(aChar == 'r'){ aChar = '\r'; } if(aChar == 'n'){ aChar = '\n'; } if(aChar == 'f'){ aChar = '\f'; } buffer.append(aChar); } }else{ buffer.append(aChar); } } return buffer.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy