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