项目中使用分发器进行负载均衡,获取不到客户端IP地址,纠结几天的,赐我一个答案吧
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.getRemoteAddr();
}
return ip; 这段话就麻烦不要贴上来了,不行
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.getRemoteAddr();
}
return ip; 这段话就麻烦不要贴上来了,不行
* IP震荡函数
* @author Administrator
*
*/
class Scanner extends Thread {
public int scanFlag; String regInfo = ""; public String wholeInfo = ""; public void run() {
String startIP = "192.168.1.1";
String endIP = "192.168.255.255"; int i = 0;
int j = 0; StringTokenizer strToken = new StringTokenizer(startIP, ".");
String[] startIPAddress = new String[strToken.countTokens()];
StringTokenizer strToken2 = new StringTokenizer(endIP, ".");
String[] endIPAddress = new String[strToken.countTokens()];
while (strToken.hasMoreTokens()) {
startIPAddress[i] = strToken.nextToken();
i++;
}
while (strToken2.hasMoreTokens()) {
endIPAddress[j] = strToken2.nextToken();
j++;
} Runtime run = Runtime.getRuntime(); int m = Integer.parseInt(startIPAddress[2]);
int n = Integer.parseInt(endIPAddress[2]); for (int ipNo = m; ipNo <= n; ipNo++) {
String newIP = startIPAddress[0] + "." + startIPAddress[1] + "."
+ ipNo + "." + "0-1";
String cmd = "d://nmap-4.53//nmap -sP " + newIP;
try {
Process p = run.exec(cmd);
BufferedInputStream in = new BufferedInputStream(p
.getInputStream());
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String temp;
while ((temp = br.readLine()) != null) {
if (temp.startsWith("Host")) {
String routeIP = startIPAddress[0] + "."
+ startIPAddress[1] + "." + ipNo + "." + "1";
wholeInfo = wholeInfo + routeIP + "/n";
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
} scanFlag = 1;
Calendar todaysDate = new GregorianCalendar();
int year = todaysDate.get(Calendar.YEAR);
int month = todaysDate.get(Calendar.MONTH) + 1;
int day = todaysDate.get(Calendar.DAY_OF_MONTH);
String regDate = "注册日期:" + year + "年" + month + "月" + day + "日"; regInfo = "本机IP:" + getLocalIP() + "/n" + "MAC地址:" + getLocalMac()
+ "/n" + "子网掩码:" + getSubMask() + "/n" + "路由地址:" + "/n"
+ wholeInfo + regDate; writeTxt();
}
/**
* 获得本机IP
* @return localIP
*/
public String getLocalIP() {
InetAddress inet = null;
String localIP = null;
String ip = null;
int num = 0; try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
} ip = inet.toString();
num = ip.indexOf("/");
localIP = ip.substring(num + 1); return localIP;
} /**
* 获得MAC地址
* @return macAddress
*/
public String getLocalMac() {
String macAddressTemp = "";
String[] macAddress = null;
int j = 0; try {
String cmd = "ipconfig /all";
Runtime run = Runtime.getRuntime();
Process p = run.exec(cmd);
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line;
int i = 0;
while ((line = br.readLine()) != null) {
if ((i = line.indexOf("Physical Address")) != -1) {
String temp = line.substring(i + 36);
macAddressTemp = macAddressTemp + temp + "/n";
}
} macAddress = macAddressTemp.split("/n"); //有些机子装有双网卡或虚拟机,相应会多出几个mac地址,取最后一个本地连接的mac地址
j = macAddress.length;
} catch (IOException e) {
e.printStackTrace();
}
return macAddress[j - 1];
}
/**
* 获取子网掩码
* @return subnetMask
*/
public String getSubMask() {
String subnetMaskTemp = "";
String[] subnetMask = null;
int j = 0; try {
String cmd = "ipconfig /all";
Runtime run = Runtime.getRuntime();
Process p = run.exec(cmd);
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line;
int i = 0;
while ((line = br.readLine()) != null) {
if ((i = line.indexOf("Subnet Mask")) != -1) {
String temp = line.substring(i + 36);
subnetMaskTemp = subnetMaskTemp + temp + "/n";
}
} subnetMask = subnetMaskTemp.split("/n");
j = subnetMask.length;
} catch (IOException e) {
e.printStackTrace();
} return subnetMask[j - 1];
} public void writeTxt() {
File file = new File("E://register. xdja"); //写入E:/register. xdja这个文件中 try {
file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(regInfo);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
InetAddress.getLocalHost();
这个有效果不哦。。
请注意大小写是否匹配?有些分发器的是X-Forwarded-For
String netIP = null;//外网IP
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean foundNet = false;//是否找到外网IP
while (netInterfaces.hasMoreElements() && !foundNet){
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress() &&
!ip.isLoopbackAddress() &&
ip.getHostAddress().indexOf(":") == -1){//外网IP
netIP = ip.getHostAddress();
foundNet = true;
break;
} else if (ip.isSiteLocalAddress() &&
!ip.isLoopbackAddress() &&
ip.getHostAddress().indexOf(":") == -1) {//内网IP
localIP = ip.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
if (netIP != null && !"".equals(netIP)) {
return netIP;
} else {
return localIP;
}