java中,如何获取真实的IP地址?
发布网友
发布时间:2022-04-24 16:58
我来回答
共3个回答
热心网友
时间:2022-04-27 17:54
/**
* 获取客户端ip
* @param request
* @return
*/
HttpServletRequest request = ServletActionContext.getRequest();
String address = request.getHeader("X-Forwarded-For");
if (address != null && this.isIpAddress(address)) {
return address;
}
return request.getRemoteAddr();
热心网友
时间:2022-04-27 19:12
import java.net.InetAddress;
import java.net.UnknownHostException;
public class NsLookup {
static public void main(String[] args){
try {InetAddress address = InetAddress.getByName(args[0]);
System.out.println(args[0]+": "+address.getHostAddress());
}catch(UnknownHostException uhe) {
System.err.println("Unable to find: "+args[0]);
}
}
}
热心网友
时间:2022-04-27 20:46
public static String getRemoteIP(HttpServletRequest request) { if (request.getHeader("x-forwarded-for") == null) { return request.getRemoteAddr(); } return request.getHeader("x-forwarded-for"); }