重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java代码获得公网ip javaweb获取ip地址

JAVA获取外网IP

请参考以下代码,可以获得任意给定网卡的ip地址:

创新互联长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为乌审企业提供专业的网站设计、成都网站建设乌审网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。

import java点虐 .InetAddress;

import java点虐 .NetworkInterface;

import java点虐 .SocketException;

import java.util.Enumeration;

public class InternetTest {

public static void main(String[] args) {

String netCard = "lo";

try {

EnumerationNetworkInterface netInterfaces = NetworkInterface

.getNetworkInterfaces();

if (netInterfaces.hasMoreElements()) {

NetworkInterface netInterface = netInterfaces.nextElement();

if (netCard.equals(netInterface.getName())) {

// 子接口,linux下会取到父接口??

EnumerationNetworkInterface subnetInterfaces = netInterface

.getSubInterfaces();

while (subnetInterfaces.hasMoreElements()) {

NetworkInterface subnetInterface = subnetInterfaces

.nextElement();

System.out.println(subnetInterface.getName());

EnumerationInetAddress subaddresses = netInterface

.getInetAddresses();

while (subaddresses.hasMoreElements()) {

InetAddress subaddress = subaddresses.nextElement();

System.out.println(subaddress.getHostAddress());

}

}

// 打印接口下所有IP

System.out.println(netInterface.getName());

EnumerationInetAddress addresses = netInterface

.getInetAddresses();

while (addresses.hasMoreElements()) {

InetAddress address = addresses.nextElement();

System.out.println(address.getHostAddress());

}

}

}

} catch (SocketException e) {

e.printStackTrace();

}

}

}

java中如何获取到本机的外网ip地址?

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

import java.io.IOException;

import java.io.InputStream;

import java点虐 .HttpURLConnection;

import java点虐 .MalformedURLException;

import java点虐 .URL;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* 获取本机外网IP地址

* 思想是访问网站,得到返回的文本后解析出本机在外网的IP地址

* @author pieryon

*

*/

public class ExternalIpAddressFetcher {

// 外网IP提供者的网址

private String externalIpProviderUrl;

// 本机外网IP地址

private String myExternalIpAddress;

public ExternalIpAddressFetcher(String externalIpProviderUrl) {

this.externalIpProviderUrl = externalIpProviderUrl;

String returnedhtml = fetchExternalIpProviderHTML(externalIpProviderUrl);

parse(returnedhtml);

}

/**

* 从外网提供者处获得包含本机外网地址的字符串

* 从返回的字符串如下

* htmlheadtitleCurrent IP Check/title/headbodyCurrent IP Address: 123.147.226.222/body/html

* @param externalIpProviderUrl

* @return

*/

private String fetchExternalIpProviderHTML(String externalIpProviderUrl) {

// 输入流

InputStream in = null;

// 到外网提供者的Http连接

HttpURLConnection httpConn = null;

try {

// 打开连接

URL url = new URL(externalIpProviderUrl);

httpConn = (HttpURLConnection) url.openConnection();

// 连接设置

HttpURLConnection.setFollowRedirects(true);

httpConn.setRequestMethod("GET");

httpConn.setRequestProperty("User-Agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

// 获取连接的输入流

in = httpConn.getInputStream();

byte[] bytes=new byte[1024];// 此大小可根据实际情况调整

// 读取到数组中

int offset = 0;

int numRead = 0;

while (offset bytes.length

(numRead=in.read(bytes, offset, bytes.length-offset)) = 0) {

offset += numRead;

}

// 将字节转化为为UTF-8的字符串

String receivedString=new String(bytes,"UTF-8");

// 返回

return receivedString;

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

in.close();

httpConn.disconnect();

} catch (Exception ex) {

ex.printStackTrace();

}

}

// 出现异常则返回空

return null;

}

/**

* 使用正则表达式解析返回的HTML文本,得到本机外网地址

* @param html

*/

private void parse(String html){

Pattern pattern=Pattern点抗 pile("(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})", Pattern.CASE_INSENSITIVE);

Matcher matcher=pattern.matcher(html);

while(matcher.find()){

myExternalIpAddress=matcher.group(0);

}

}

/**

* 得到本机外网地址,得不到则为空

* @return

*/

public String getMyExternalIpAddress() {

return myExternalIpAddress;

}

public static void main(String[] args){

ExternalIpAddressFetcher fetcher=new ExternalIpAddressFetcher("");

System.out.println(fetcher.getMyExternalIpAddress());

}

}

JAVA如何获得外网IP地址?

java获取外网ip地址方法:

public class Main {

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

System.out.println(Main.getRealIp());

}

public static String getRealIp() throws SocketException {

String localip = null;// 本地IP,如果没有配置外网IP则返回它

String netip = null;// 外网IP

EnumerationNetworkInterface netInterfaces =

NetworkInterface.getNetworkInterfaces();

InetAddress ip = null;

boolean finded = false;// 是否找到外网IP

while (netInterfaces.hasMoreElements() !finded) {

NetworkInterface ni = netInterfaces.nextElement();

EnumerationInetAddress address = ni.getInetAddresses();

while (address.hasMoreElements()) {

ip = address.nextElement();

if (!ip.isSiteLocalAddress()

!ip.isLoopbackAddress()

ip.getHostAddress().indexOf(":") == -1) {// 外网IP

netip = ip.getHostAddress();

finded = true;

break;

} else if (ip.isSiteLocalAddress()

!ip.isLoopbackAddress()

ip.getHostAddress().indexOf(":") == -1) {// 内网IP

localip = ip.getHostAddress();

}

}

}

if (netip != null !"".equals(netip)) {

return netip;

} else {

return localip;

}

}

}


网站栏目:java代码获得公网ip javaweb获取ip地址
文章分享:http://cqcxhl.cn/article/ddejjsh.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP