获取本机外网ip地址

package com.ning;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;

public class Listip {

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

    System.out.println("本机的外网IP是:"   

    //+ Listip.getWebIp("http://iframe.ip138.com/ic.asp"));  

     //+ Listip.getWebIp("http://www.ip138.com"));   

    + Listip.getWebIp("http://1111.ip138.com/ic.asp"));

   }  

  public static String getWebIp(String strUrl) {   

    try {    

      URL url = new URL(strUrl);    

      BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"GB2312"));   

       String s = "";   

       StringBuffer sb = new StringBuffer("");    

      String webContent = "";   

       while ((s = br.readLine()) != null) {     

        sb.append(s + "rn");   

       }   

       br.close();    

      webContent = sb.toString();    

      int start = webContent.indexOf("[") + 1;    

      int end = webContent.indexOf("]");    

      System.out.println("webContent=" + webContent);   

       System.out.println("start=" + start);    

      System.out.println("end=" + end);   

       if (start < 0 || end < 0) {     

        return null;     

      }   

       webContent = webContent.substring(start, end);   

       return webContent;   

    } catch (Exception e) {    

      e.printStackTrace();    

      return "error open url:" + strUrl;   

    }

 }

}

转载于:https://www.cnblogs.com/ning2015/p/4742849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值