
private String getIpAddr() {
String ipAddress = null;
//ipAddress = thisgetRequest()getRemoteAddr();
ipAddress = thisgetRequest()getHeader("x-forwarded-for");
if(ipAddress == null || ipAddresslength() == 0 || "unknown"equalsIgnoreCase(ipAddress)) {
ipAddress = thisgetRequest()getHeader("Proxy-Client-IP");
}
if(ipAddress == null || ipAddresslength() == 0 || "unknown"equalsIgnoreCase(ipAddress)) {
ipAddress = thisgetRequest()getHeader("WL-Proxy-Client-IP");
}
if(ipAddress == null || ipAddresslength() == 0 || "unknown"equalsIgnoreCase(ipAddress)) {
ipAddress = thisgetRequest()getRemoteAddr();
if(ipAddressequals("127001")){
//根据网卡取本机配置的IP
InetAddress inet=null;
try {
inet = InetAddressgetLocalHost();
} catch (UnknownHostException e) {
eprintStackTrace();
}
ipAddress= inetgetHostAddress();
}
}
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if(ipAddress!=null && ipAddresslength()>15){ //""length() = 15
if(ipAddressindexOf(",")>0){
ipAddress = ipAddresssubstring(0,ipAddressindexOf(","));
}
}
return ipAddress;
}
我才用的一个项目,requestgetRemoteAddr();
不是很准确
方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmdexe /c ipconfig | findstr IPv4";
try {
Process p = RuntimegetRuntime()exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(pgetInputStream()));
String line = null;
while((line = brreadLine()) != null){
line = linesubstring(linelastIndexOf(":")+2,linelength());
sbappend(line);
}
brclose();
pdestroy();
} catch (IOException e) {
eprintStackTrace();
}
return sbtoString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
Enumeration<NetworkInterface> en = NetworkInterfacegetNetworkInterfaces();
while (enhasMoreElements()) {
NetworkInterface intf = (NetworkInterface) ennextElement();
Enumeration<InetAddress> enumIpAddr = intfgetInetAddresses();
while (enumIpAddrhasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddrnextElement();
if (!inetAddressisLoopbackAddress() && !inetAddressisLinkLocalAddress()
&& inetAddressisSiteLocalAddress()) {
sbappend(inetAddressgetHostAddress()toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sbtoString();
}
import javanet;
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InetAddress ia=null;
try {
ia=iagetLocalHost();
String localname=iagetHostName();
String localip=iagetHostAddress();
Systemoutprintln("本机名称是:"+ localname);
Systemoutprintln("本机的ip是 :"+localip);
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
import javautil;
import javanet;public class getMyIP
{
public static void main(String[] args)
{
String IP = null;
String host = null;
try
{
InetAddress ia = InetAddressgetLocalHost();
host = iagetHostName();//获取计算机名字
IP= iagetHostAddress();//获取IP
}
catch(UnknownHostException e)
{
eprintStackTrace();
}
Systemoutprintln(host);
Systemoutprintln(IP);
}
}
Java编程查询IP地址归属地,可以调用淘宝提供的service查询,并且解析>package getAddressByIp;
import javaioByteArrayOutputStream;
import javaioIOException;
import javaioInputStream;
import javanet>
可以使用 InetAddressgetLocalHost(),代码如下:
import javanet;public class App {
public static void main(String[] args) throws UnknownHostException {
InetAddress local = InetAddressgetLocalHost();
Systemoutprintln("计算机名:" + localgetHostName());
Systemoutprintln("IP:" + localgetHostAddress());
}
}
以上就是关于JAVA求教SSH项目中如何获取外来访问者的IP地址全部的内容,包括:JAVA求教SSH项目中如何获取外来访问者的IP地址、java中获取本地IP地址、如何用 Java 获取系统 IP等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)