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