java获取ip地址

Java code

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ip extends JFrame
  implements ActionListener
{
  private static final long serialVersionUID = 3339481369781127417L;
  JButton jb1;
  JButton jb2;
  JButton jb3;
  JPanel jp;
  JLabel jl;
  JLabel jl1;
  JTextField jt;

  public ip()
  {
    this.jp = new JPanel();
    this.jl = new JLabel();
    this.jl1 = new JLabel("您的域名:");
    this.jb1 = new JButton("提交");
    this.jb2 = new JButton("重置");
    this.jb3 = new JButton("退出");
    this.jt = new JTextField(20);
    this.jb1.addActionListener(this);
    this.jb2.addActionListener(this);
    this.jb3.addActionListener(this);
    this.jp.setLayout(new GridLayout(3, 2));
    this.jp.add(this.jl1);
    this.jp.add(this.jt);
    this.jp.add(this.jb1);
    this.jp.add(this.jl);
    this.jp.add(this.jb2);
    this.jp.add(this.jb3);

    setBounds(200, 200, 500, 240);
    add(this.jp);
    setVisible(true);
    setDefaultCloseOperation(3);
  }

  public static void main(String[] args)
  {
    new ip();
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.jb1) {
      String url = this.jt.getText();
      InetAddress ip = null;
      try {
        ip = InetAddress.getByName(url);
      }
      catch (UnknownHostException e1) {
        e1.printStackTrace();
      }
      this.jl.setText(ip.toString());
    }
    else if (e.getSource() == this.jb2) {
      this.jl.setText("");
      this.jt.setText("");
    } else {
      System.exit(0);
    }
  }
}


### 获取本机IP地址的方法 在Java中,可以通过使用`InetAddress`类来获取本地系统的IP地址。以下是一个简单的示例代码,展示了如何获取并打印本地主机的IP地址和主机名: ```java import java.net.InetAddress; public class Main { public static void main(String[] args) throws Exception { InetAddress addr = InetAddress.getLocalHost(); System.out.println("Local HostAddress: " + addr.getHostAddress()); System.out.println("Local host name: " + addr.getHostName()); } } ``` 这段代码使用了`InetAddress.getLocalHost()`方法来获取本地主机的`InetAddress`对象,然后通过调用`getHostAddress()`和`getHostName()`方法分别获取IP地址和主机名[^2]。 ### 获取公网IP地址的方法 如果需要获取公网IP地址,可以通过调用第三方API来实现。例如,可以使用`https://api.ipify.org?format=json`接口来获取公网IP地址: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class PublicIPFetcher { public static void main(String[] args) { try { URL url = new URL("https://api.ipify.org?format=json"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close(); connection.disconnect(); // 解析JSON响应 String jsonResponse = content.toString(); String publicIP = jsonResponse.split("\"")[3]; // 简单解析JSON System.out.println("Public IP Address: " + publicIP); } catch (Exception e) { e.printStackTrace(); } } } ``` 这段代码通过发送HTTP GET请求到`api.ipify.org`,然后读取返回的JSON格式的公网IP地址[^1]。 ### 注意事项 - 获取本地IP地址时,需要注意网络配置是否正确,以及是否有多个网络接口。 - 获取公网IP地址时,需要确保网络连接正常,并且能够访问外部API服务。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值