Network Programming--url Programming

Source: Internet
Author: User

URL: is the abbreviation for the Uniform Resource Locator, which represents the address of a resource on the Internet. Through the URL we can access various network resources on the Internet, such as the most common www,ftp site. The browser can find the appropriate file or other resource on the network by parsing the given URL.

composition : The basic structure of the URL consists of 5 parts.

< transport protocol >://< host name >:< port number >/< file name >#< reference >

Http://www.tomcat.com:80/Gamelan/network.htm#BOTTOM

Protocol name://Machine name + port number + file name + Internal Reference

In order to indicate that the class URL is implemented in the Url,java.net package.

Construction method

①url (String URL)

The URL represents an absolute address, and the URL object points directly to the resource, such as:

URL url=new url (http://www.baidu.com);

②url (URL baseurl,string relativeurl)

where BaseURL represents an absolute address, relativeurl represents a relative address, such as:

URL url=new url (http://www.baidu.com);

URL lib=new url (url, "library/library.htm");

③url (String protocol,string host,string file)

Where protocol represents the communication protocol, host represents the hostname, and file represents the file name, such as:

New URL ("http", www.baidu.com, "/test/test.htm");

④url (String protocol,string host,int port,string file)

Url lib = new URL ("http", www.baidu.com,80, "/test/test.htm");

Gets the properties of the URL object

①getfile (): Gets the full file name of the resource specified by the URL.

②gethost (): Returns the host name.

③getpath (): Returns the file directory and file name of the specified resource.

④getport (): Returns the port number.

⑤getprotocol (): Returns a String object representing the protocol in the URL.

⑥getref (): Returns the HTML document tag in the URL, which is the # sign.

⑦getuserinfo (): Returns the user information.

There are two ways in which URLs can be used to access resources on Interent.

① uses the URL's OpenConnection () method to create an object of the URLConnection class. The corresponding input/output stream is then obtained through the URLConnection object.

② takes advantage of the OpenStream () method of the URL class. The OpenStream () method establishes a connection to the specified URL and returns an object of the InputStream class.

Example: Download a URL file to a specified directory

Download method:

1 classDownloadutil {2      Public Static voidDownload (String urlstring, String fileName,3String Savepath)throwsIOException {4URL url =NewURL (urlstring);5         //Method One6         //URLConnection conn = Url.openconnection ();7         //InputStream is = Conn.getinputstream ();8         9         //Method TwoTenInputStream is =Url.openstream (); One          A         byte[] Buff =New byte[1024]; -         intLen = 0; -File File =NewFile (savepath); the         if(!file.exists ()) { - file.mkdirs (); -         } -OutputStream OS =NewFileOutputStream (File.getabsolutepath () + "\ \" ++fileName); -          while(len = is.read (buff))! =-1) { +Os.write (Buff, 0, Len); A         } at         //Freeing Resources - os.close (); - is.close (); -     } -}

Main method:

1         Try {2 Downloadutil3 . Download (4"Http://images.sohu.com/saf/a2016/0511/wKiDRFcy-16AHkkXAABH52NJX18606.jpg",5"My.jpg", "D:/ceshi");6}Catch(IOException e) {7 e.printstacktrace ();8}

Network Programming--url Programming

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.