Get the local public network IP and intranet IP (JAVA source code) __java

Source: Internet
Author: User
Tags stringbuffer
public class Test {public static void main (string[] args) {try {String ip1 = Getmyip ();
			System.out.println ("MyIP:" + ip1);
			String ip2 = getmyiplocal ();
		System.out.println ("Mylocalip:" + ip2);
		catch (IOException E1) {e1.printstacktrace ();
		} private static String Getmyip () throws IOException {InputStream ins = null;
			try {URL url = new URL ("http://iframe.ip138.com/ic.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 (); }} private static String getmyiplocal() throws IOException {inetaddress ia = inetaddress.getlocalhost ();
	return ia.gethostaddress ();
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.