用Java获取IP [英] Getting IP in Java
本文介绍了用Java获取IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用Java获取IP地址的最佳方法是什么?我正在尝试getLocalHost()
,但是它返回了我的计算机IP地址.我想要类似此的内容.
我也试图从类似的服务中通过HTML获取IP,但是我认为这不是一个好主意.
What is the best way to get IP address in Java? I was trying getLocalHost()
, but it returns my computer IP addrees. I want something like this.
Also I was trying to get IP by HTML from services like that, but I think it's not good idea.
推荐答案
以下内容使用Amazon Web服务并为我工作.
The following uses Amazon web services and works for me.
import java.net.*;
import java.io.*;
public class IPTest{
public static void main(String args[]) throws Exception{
URL whatismyip = new URL("http://checkip.amazonaws.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(
whatismyip.openStream()));
String ip = in.readLine(); //you get the IP as a String
System.out.println("My IP address:"+ip);
}
}
这篇关于用Java获取IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文