HttpClient Request Server Picture

Source: Internet
Author: User
Tags file copy

Let's first introduce an IO-stream-related jar package,

Download from Apache

After download, jar package and source code

We only need to bring the jar package into the project:

We then use the Fileutils class, which has a file copy method.

We save the requested picture on the local disk

The code is as follows:

Package Com.zhi.httpclient3;import Java.io.file;import Java.io.inputstream;import org.apache.commons.io.FileUtils; Import Org.apache.http.httpentity;import Org.apache.http.client.methods.closeablehttpresponse;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.closeablehttpclient;import Org.apache.http.impl.client.httpclients;public class Demo {public static void main (string[] args) throws Exception { Closeablehttpclient Closeablehttpclient=httpclients.createdefault (); 1, create an instance httpget httpget=new httpget ("http://static.bootcss.com/www/assets/img/codeguide.png?1505127079951"); 2, create an instance closeablehttpresponse Closeablehttpresponse=closeablehttpclient.execute (HttpGet); 3, the implementation of Httpentity httpentity=closeablehttpresponse.getentity (); 4. Get entity if (httpentity!=null) {System.out.println ("ContentType:" +httpentity.getcontenttype (). GetValue ()); I Nputstream inputstream=httpentity.getcontent (); Fileutils.copytofile (InputStream, New File ("D://xxx.png")); Save the picture in the disk D drive, named Xxx.png}Closeablehttpresponse.close (); Closeablehttpclient.close ();}} 

After running, a picture file will be added to the D drive.

Similarly, JPG and GIF can also be crawled after being saved locally.

HttpClient Request Server Picture

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.