用Java獲取本機IP或者請求使用者的真正IP地址
阿新 • • 發佈:2021-11-01
一、在Web請求中獲取請求使用者的IP地址
public static String getUserRealIP(HttpServletRequest request) throws UnknownHostException { String ip = ""; // 有的user可能使用代理,為處理使用者使用代理的情況,使用x-forwarded-for if (request.getHeader("x-forwarded-for") == null) { ip = request.getRemoteAddr(); }else { ip = request.getHeader("x-forwarded-for"); } if ("127.0.0.1".equals(ip)) { // 獲取本機真正的ip地址 ip = InetAddress.getLocalHost().getHostAddress(); } return ip; }
二、獲取本機IP地址和主機名
import java.util.*; import java.net.*; publicclass getMyIP { public static void main(String[] args) { String ip = null; String host = null; try{ InetAddress ia = InetAddress.getLocalHost(); host = ia.getHostName();//獲取計算機名字 ip = ia.getHostAddress();//獲取IP } catch(UnknownHostException e) { e.printStackTrace(); } System.out.println(host); System.out.println(IP); } }
三、網上搜索的方法
import javax.servlet.http.HttpServletRequest; /** * 自定義訪問物件工具類 * * 獲取物件的IP地址等資訊 * @author X-rapido * */ public class CusAccessObjectUtil { /** * 獲取使用者真實IP地址,不使用request.getRemoteAddr();的原因是有可能使用者使用了代理軟體方式避免真實IP地址, * * 可是,如果通過了多級反向代理的話,X-Forwarded-For的值並不止一個,而是一串IP值,究竟哪個才是真正的使用者端的真實IP呢? * 答案是取X-Forwarded-For中第一個非unknown的有效IP字串。 * * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, * 192.168.1.100 * * 使用者真實IP為: 192.168.1.110 * * @param request * @return */ public static String getIpAddress(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_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.getRemoteAddr(); if("127.0.0.1".equals(ip)||"0:0:0:0:0:0:0:1".equals(ip)){ //根據網絡卡取本機配置的IP InetAddress inet=null; try { inet = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } ip= inet.getHostAddress(); } } return ip; } }
四、擴充套件知識
Java獲取客戶端請求電腦IP,作業系統等等各種資訊
HttpServletRequest request = ServletActionContext.getRequest(); System.out.println("瀏覽器基本資訊:"+request.getHeader("user-agent")); System.out.println("客戶端系統名稱:"+System.getProperty("os.name")); System.out.println("客戶端系統版本:"+System.getProperty("os.version")); System.out.println("客戶端作業系統位數:"+System.getProperty("os.arch")); System.out.println("HTTP協議版本:"+request.getProtocol()); System.out.println("請求編碼格式:"+request.getCharacterEncoding()); System.out.println("Accept:"+request.getHeader("Accept")); System.out.println("Accept-語言:"+request.getHeader("Accept-Language")); System.out.println("Accept-編碼:"+request.getHeader("Accept-Encoding")); System.out.println("Connection:"+request.getHeader("Connection")); System.out.println("Cookie:"+request.getHeader("Cookie")); System.out.println("客戶端發出請求時的完整URL"+request.getRequestURL()); System.out.println("請求行中的資源名部分"+request.getRequestURI()); System.out.println("請求行中的引數部分"+request.getRemoteAddr()); System.out.println("客戶機所使用的網路埠號"+request.getRemotePort()); System.out.println("WEB伺服器的IP地址"+request.getLocalAddr()); System.out.println("WEB伺服器的主機名"+request.getLocalName()); System.out.println("客戶機請求方式"+request.getMethod()); System.out.println("請求的檔案的路徑"+request.getServerName()); System.out.println("請求體的資料流"+request.getReader()); BufferedReader br=request.getReader(); String res = ""; while ((res = br.readLine()) != null) { System.out.println("request body:" + res); } System.out.println("請求所使用的協議名稱"+request.getProtocol()); System.out.println("請求中所有引數的名字"+request.getParameterNames()); Enumeration enumNames= request.getParameterNames(); while (enumNames.hasMoreElements()) { String key = (String) enumNames.nextElement(); System.out.println("引數名稱:"+key); }
五、在本機上獲取計算機資訊
public void getIP(){ try { InetAddress address = InetAddress.getLocalHost(); String ip = address.getHostAddress().toString();//獲取本機IP String hostName = address.getHostName().toString();//獲取本機計算機名稱 System.out.println("ip is:"+ip); System.out.println("host name is:"+hostName); } catch (UnknownHostException e) { e.printStackTrace(); } }