Java获取本地服务器的IP


System.out.println("getRequestURL:"+request.getRequestURL());
System.out.println("getLocalAddr:"+request.getLocalAddr());
System.out.println("getLocalPort:"+request.getLocalPort());
System.out.println("getRequestURI:"+request.getRequestURI());
System.out.println("getScheme:"+request.getScheme());
System.out.println("getServerPort:"+request.getServerPort());
System.out.println("getServletPath:"+request.getServletPath());
try {
InetAddress addr = InetAddress.getLocalHost();
byte[] ipAddr = addr.getAddress();
String ipAddrStr = "";
for (int i = 0; i < ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i] & 0xFF;
}
System.out.println(ipAddrStr);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
//输出:
/*
getRequestURL:http://localhost:8080/TAWM/electronicContract/web/upload
getLocalAddr:0.0.0.0
getLocalPort:8080
getRequestURI:/TAWM/electronicContract/web/upload
getScheme:http
getServerPort:8080
getServletPath:/electronicContract/web/upload
192.168.1.50
*/


参考博文:[url]http://kata520-java.iteye.com/blog/1013302[/url]

你可能感兴趣的:(web技术零碎)