java获得IP地址

java获得IP地址,第1张

下面有一篇文章,介绍若何读取物理网卡地址 ,同样的

你可以用这个方法读取你所需要的本机IP地址

=======================================================

J2SE50新特性之ProcessBuilder

这个例子使用了J2SE50的ProcessBuilder类执行外部的程序,相对于 Runtimeexec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址

package comkuaffjdk5package;

import javaioIOException;

import javaioInputStream;

import javautilArrayList;

import javautilList;

public class ProcessBuilderShow

{

public static List getPhysicalAddress()

{

Process p = null;

//物理网卡列表

List address = new ArrayList();

try

{

//执行ipconfig /all命令

p = new ProcessBuilder("ipconfig", "/all")start();

}

catch (IOException e)

{

return address;

}

byte[] b = new byte[1024];

StringBuffer sb = new StringBuffer();

//读取进程输出值

InputStream in = pgetInputStream();

try

{

while (inread(b)>0)

{

sbappend(new String(b));

}

}

catch (IOException e1)

{

}

finally

{

try

{

inclose();

}

catch (IOException e2)

{

}

}

//以下分析输出值,得到物理网卡

String rtValue = sbsubstring(0);

int i = rtValueindexOf("Physical Address :");

while(i>0)

{

rtValue = rtValuesubstring(i + "Physical Address :"length());

addressadd(rtValuesubstring(0,18));

i = rtValueindexOf("Physical Address :");

}

return address;

}

public static void main(String[] args)

{

List address = ProcessBuilderShowgetPhysicalAddress();

for(String add:address)

{

Systemoutprintf("物理网卡地址:%s%n", add);

}

}

}

首先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

/

获取客户端ip

@param request

@return

/

>

java获取本机的外网ip示例:

import javaioIOException;

import javaioInputStream;

import javanet>

import javanetInetAddress;

import javanetUnknownHostException;

public class Test

{

public static void main(String[] args) throws UnknownHostException

{

InetAddress inet = InetAddressgetLocalHost();

Systemoutprintln("本机的ip=" + inetgetHostAddress());

}

}

一般情况下,把所有的方法试一遍,看哪个是你想要的路径:

Systemoutprintln(ThreadcurrentThread()getContextClassLoader()getResource(""));

Systemoutprintln(YourClassclassgetClassLoader()getResource(""));

Systemoutprintln(ClassLoadergetSystemResource(""));

Systemoutprintln(YourClassclassgetResource(""));

Systemoutprintln(YourClassclassgetResource("/"));

Systemoutprintln(new File("")getAbsolutePath());

Systemoutprintln(SystemgetProperty("userdir"));

肯定有一个中,如果出来jar的根目录就自己加路径名,比如出来的是class文件夹,而你要的东西在class/resource/config/你就往后拼字符串就好了

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(); } }

是否可以解决您的问题?

import javanet;

public class Test {

public static void main(String[] args) throws Exception {

String ip = InetAddressgetLocalHost()getHostAddress();

Systemoutprintln(ip);

}

}

以上就是关于java获得IP地址全部的内容,包括:java获得IP地址、JAVA怎么获取服务器IP、java中,如何获取真实的IP地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9796221.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存