【问题标题】:how to get the client's IP? [duplicate]如何获取客户端的IP? [复制]
【发布时间】:2015-04-28 10:49:27
【问题描述】:

我的代码总是返回环回 ip 而不是我的 IP 是 192.168。.

try {

            ip = InetAddress.getLocalHost();
            System.out.println("Current IP address : " + ip.getHostAddress());
          } catch (UnknownHostException e) {

            e.printStackTrace();

          }

【问题讨论】:

  • 你能解释一下我的代码总是返回环回
  • 也许这篇文章可以帮到你:stackoverflow.com/questions/9481865/…
  • 他获取的是私有网络上计算机的环回(内部)IP 地址,而不是公共 IP 地址。但是这个问题是重复的。
  • 我总是得到 127.0.1.1

标签: java ip


【解决方案1】:

有些网站,其唯一的数据是一行,其中包括您的公共 IP 地址。

尝试类似:

URLConnection con = new URL("http://www.myipaddress.com").openConnection();
String ip = new BufferedReader(new 
                    InputStreamReader(con.getInputStream())).readLine();

【讨论】:

  • OP想用java做,所以贴了java代码,我觉得你应该用java回答
  • @SpringLearner 希望对您有所帮助。
  • 我认为你的代码不会编译
  • 可以编译运行
  • HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();字符串 ip = httpServletRequest.getRemoteAddr(); System.out.println(ip);
猜你喜欢
  • 2013-05-01
  • 2012-03-31
  • 2014-11-12
  • 2014-04-14
  • 2017-07-06
  • 2016-10-05
  • 1970-01-01
  • 2012-03-14
  • 2012-02-16
相关资源
最近更新 更多