Java Get URL address picture

Source: Internet
Author: User

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

Related Article

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.