获取公网ip /** * 获取公网ip * return String * */private static String getMyIP() throws IOException {InputStream ins = null;try {URL url = new URL("http://www.ip138.com/ip2city.asp");URLConnection con = url.openConnection();ins = con
/** * 获取公网ip * return String * */ private static String getMyIP() throws IOException { InputStream ins = null; try { URL url = new URL("http://www.ip138.com/ip2city.asp"); URLConnection con = url.openConnection(); ins = con.getInputStream(); InputStreamReader isReader = new InputStreamReader(ins, "GB2312"); BufferedReader bReader = new BufferedReader(isReader); StringBuffer webContent = new StringBuffer(); String str = null; while ((str = bReader.readLine()) != null) { webContent.append(str); } int start = webContent.indexOf("[") + 1; int end = webContent.indexOf("]"); return webContent.substring(start, end); } finally { if (ins != null) { ins.close(); } } }获取本地ip
private static String getMyIPLocal() throws IOException { InetAddress ia = InetAddress.getLocalHost(); return ia.getHostAddress(); }测试
public static void main(String[] args) { try { long beginTime = System.currentTimeMillis(); System.out.println("公网ip:"+getMyIP()); System.out.println("局域网ip:"+getMyIPLocal()); long endTime = System.currentTimeMillis(); System.out.println(endTime-beginTime); } catch (IOException e) { e.printStackTrace(); } }TIM截图20171009163741.jpg