public static void main(String[] args) { try { // 获取计算机名 String name = InetAddressgetLocalHost()getHostName(); // 获取IP地址 String ip = InetAddressgetLocalHost()getHostAddress(); Systemoutprintln("计算机名:"+name); Systemoutprintln("IP地址:"+ip); } catch (UnknownHostException e) { Systemoutprintln("异常:" + e); eprintStackTrace(); } }
是否可以解决您的问题?
/
获取客户端ip
@param request
@return
/
>
Java编程查询IP地址归属地,可以调用淘宝提供的service查询,并且解析>package getAddressByIp;
import javaioByteArrayOutputStream;
import javaioIOException;
import javaioInputStream;
import javanet>
首先IP为一个字符串,例如:
class test{
static void Split(string ip,out string str1)
{
int i=iplength;
while(i>0)
{
char ch=ip[i-1];
if(ch==':')
break;
i--;
}
str1=ipSubstring(0,i);
}
static void Main()
{
string str1;
Split("1921680255:8080",out str1)
ConsoleWriteLine("{0}",str1);
}
}
str1中保存的就是你的ip,1921680255
方法如下:
方法一,使用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();
}
以上就是关于JAVA获取IP地址全部的内容,包括:JAVA获取IP地址、java中,如何获取真实的IP地址、java IP查询方法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)