导航:首页 > 编程语言 > java查询ip

java查询ip

发布时间:2023-03-18 18:36:15

❶ 如何用 java 获取系统 IP

importjava.net.*;

publicclassTest6{

publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
InetAddressia=null;
try{
ia=ia.getLocalHost();

Stringlocalname=ia.getHostName();
Stringlocalip=ia.getHostAddress();
System.out.println("本机名称是:"+localname);
System.out.println("本机的ip是:"+localip);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

}

❷ JAVA怎么获取IP地址

java代码获取ip地址方法是
调用java.net包下面的的InetAddress类获取。

❸ java中获取本地IP地址

方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmd.exe /c ipconfig | findstr IPv4";
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
line = line.substring(line.lastIndexOf(":")+2,line.length());
sb.append(line);
}
br.close();
p.destroy();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
&& inetAddress.isSiteLocalAddress()) {
sb.append(inetAddress.getHostAddress().toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sb.toString();
}

❹ 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

Enumeration<NetworkInterface> netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> 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地址

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
* @author Becolette
* @description TODO
* @date 2015-11-5 下午01:58:46
*/
public class IpAddress {
public static String find() {
List<String> ips = new ArrayList<String>();
// 返回所有网络接口的一个枚举实例
Enumeration<?> allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
// 获得当前网络接口
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address && ip.getHostAddress().indexOf(".") != -1) {
ips.add(ip.getHostAddress());
}
}
}
if (ips.size() == 1) {
return ips.get(0);
} else {
for (String ipa : ips) {
if (!"127.0.0.1".equals(ipa)) {
return ipa;
}
}
}
return MacAddress.find();
}
}

❻ java 怎么查看局域网的ip有哪些

点击系统桌面左下角的“开始”,然后在搜索框内输入“cmd”。
注:如果是XP系统,请依次点击“开始”再点击“运行”,接着在窗口中输入“cmd”就可以了,其它步骤相同!

在弹出的CMD命令中输入:ipconfig /all,并回车。

3
之后,我们会得到如下图的信息。
这里,我们只需要查看IPv4 地址,默认网关和DNS服务器就可以了。
如图标记的一样,IPv4 地址为我们本机的IP地址,剩余的默认网管和DNS服务器也是我们本机的默认网管和DNS地址。

❼ java IP查询方法

Java编程查询IP地址归属地,可以调用淘宝提供的查询,并且解析http请求返回的json串,代码如下:

packagegetAddressByIp;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importnet.sf.json.JSONObject;

publicclassGetAddressByIp
{
/**
*
*@paramIP
*@return
*/
(StringIP){
Stringresout="";
try{
Stringstr=getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+IP);
System.out.println(str);
JSONObjectobj=JSONObject.fromObject(str);
JSONObjectobj2=(JSONObject)obj.get("data");
Stringcode=(String)obj.get("code");
if(code.equals("0")){
resout=obj2.get("country")+"--"+obj2.get("area")+"--"+obj2.get("city")+"--"+obj2.get("isp");
}else{
resout="IP地址有误";
}
}catch(Exceptione){

e.printStackTrace();
resout="获取IP地址异常:"+e.getMessage();
}
returnresout;

}

(StringurlStr)
{
try
{//获取HttpURLConnection连接对象
URLurl=newURL(urlStr);
HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();
//设置连接属性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
//获取相应码
intrespCode=httpConn.getResponseCode();
if(respCode==200)
{
returnConvertStream2Json(httpConn.getInputStream());
}
}
catch(MalformedURLExceptione)
{
e.printStackTrace();
}
catch(IOExceptione)
{
e.printStackTrace();
}
return"";
}

(InputStreaminputStream)
{
StringjsonStr="";
//ByteArrayOutputStream相当于内存输出流
ByteArrayOutputStreamout=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlen=0;
//将输入流转移到内存输出流中
try
{
while((len=inputStream.read(buffer,0,buffer.length))!=-1)
{
out.write(buffer,0,len);
}
//将内存流转换为字符串
jsonStr=newString(out.toByteArray());
}
catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnjsonStr;
}
}

❽ JAVA获取IP地址

public static void main(String[] args) { try { // 获取计算机名 String name = InetAddress.getLocalHost().getHostName(); // 获取IP地址 String ip = InetAddress.getLocalHost().getHostAddress(); System.out.println("计算机名:"+name); System.out.println("IP地址:"+ip); } catch (UnknownHostException e) { System.out.println("异常:" + e); e.printStackTrace(); } }
是否可以解决您的问题?

阅读全文

与java查询ip相关的资料

热点内容
umeng打包工具 浏览:765
g76锥度牙怎么编程 浏览:430
win10企业版关机很慢 浏览:163
微信短视频是保存在哪个文件夹 浏览:985
win10打印机设置纸张大小设置 浏览:427
卫星测控数据有哪些 浏览:451
格式工厂330教程 浏览:421
童美编程课怎么样 浏览:40
网页代码调试器 浏览:54
读取文件string 浏览:500
ug自动编程软件怎么画数控图 浏览:920
什么网站可以介绍主机 浏览:340
移动短信查询代码 浏览:192
怎么看公司网络是什么时候开通 浏览:960
cad2015文件参照怎么关闭 浏览:678
最小二乘逼近程序 浏览:610
铁路12306密码找不回 浏览:352
默认网络覆盖的脑区 浏览:319
itunes恢复iphone教程 浏览:292
炉石现在是什么版本 浏览:825

友情链接