获取外网IP

package com.apply.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/10/18.
 */
public class Demo {
private static final String S = "\\
(.*?)\\<\\/dd>"; public static void main(String[] args) throws IOException { String ip = ""; String chinaz = "http://ip.chinaz.com"; StringBuilder builder = new StringBuilder(); String temp = ""; HttpURLConnection urlConnection = (HttpURLConnection)new URL(chinaz).openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8")); while ((temp = reader.readLine())!=null){ builder.append(temp+"\n"); } if (reader != null){ reader.close(); } Pattern p = Pattern.compile(S); Matcher m = p.matcher(builder.toString()); if(m.find()){ String ipstr = m.group(1); ip = ipstr; } System.out.println(ip); } }

还有个巨简单的方法

http://ip.chinaz.com/getip.aspx

你可能感兴趣的:(获取外网IP)