Package Com.listings.web.controller; import Java.io.datainputstream;import Java.io.dataoutputstream;import Java.io.fileoutputstream;import Java.net.httpurlconnection;import Java.net.url;import java.util.Date; Public classCatchpic { Public StaticBoolean Saveurlas (String fileUrl, String savepath)/*FileURL network resource address*/ { Try { /*Assigning a network resource address to a URL*/URL url=NewURL (FILEURL); /*This is a fixed-format usage of the network resource for the contact, so that subsequent in variables get the input stream of the URL interception network resource*/httpurlconnection Connection=(HttpURLConnection) url.openconnection (); DataInputStreaminch=NewDataInputStream (Connection.getinputstream ()); /*also available here are the paths that Bufferedinputstream and bufferedoutputstream need to save*/DataOutputStream out=NewDataOutputStream (NewFileOutputStream (Savepath)); /*Savepath The parameter to the address specified by the out output stream where the stored local address of the captured picture is assigned*/ byte[] buffer =New byte[4096]; intCount =0; while(Count =inch. Read (buffer)) >0)/*reads the input stream as a byte and writes to buffer*/ { out. write (buffer,0, Count); } out. Close ();/*The following three behaviors turn off the input and output stream and the fixed format of network resources*/ inch. Close (); Connection.disconnect (); return true;/*Network resource interception and store local success return True*/ } Catch(Exception e) {System. out. println (e + fileUrl +Savepath); return false; } } Public Static voidMain (string[] args) {catchpic pic=NewCatchpic ();/*Create an instance*/ //URLs that need to be downloadedString Photourl ="http://photos.listhub.net/GAMLS/07442715/25?lm=20150426T002920"; //intercept the last/subsequent stringString FileName =NewDate (). GetTime () +". PNG"; //Picture Save PathString FilePath ="d:/img/"; /*call the function and pass the argument*/Boolean flag= Pic.saveurlas (Photourl, FilePath +fileName); System. out. println ("Run ok!\n Get URL file"+flag); System. out. println (FilePath); System. out. println (FileName); } }
Java Get URL address picture