HttpClient 4 File Download

Source: Internet
Author: User

Automated tests have been done for a long time, leaving without pay.

In fact, I didn't know what httpclient version. Just checked the next. M2 directory, originally in Org.apache.httpcomponents under. Look at the next version, is the 4.1-ALPHA1.

In fact, my automation and development is no different Ah, are also code ah, mainly Java, it's mostly junit. Actually, it's harder than the development, the Web group, the Platform group, the client group. Basically who wrote the function I have to ask someone. Communication is very important. Although there are no barriers between colleagues, But they are also to help you ah, not the obligation ah. When others have a busy time, they will also have time to forget, and sometimes you will be delayed. So the work is not good, often depends on others have no time ah.

No more nonsense, here's the code:

import java.io.file;   import java.io.fileoutputstream;   import  java.io.ioexception;   import java.io.inputstream;      import  org.apache.http.httpentity;   import org.apache.http.httphost;   import  org.apache.http.httpresponse;   import org.apache.http.httpstatus;   import  org.apache.http.client.clientprotocolexception;   import org.apache.http.client.httpclient;    import org.apache.http.client.methods.httpget;   import  org.apache.http.impl.client.defaulthttpclient;      Public class searchdomain  {          public static void main (string[]  args)  throws ClientProtocolException, IOException {            //Instantiate a httpclient     &nBsp;     httpclient httpclient = new defaulthttpclient ();            //Set Target site   web default port 80 can not write   of course, if the other port should be marked                                                                          HttpHost  Httphost = new httphost ("www.shanhe114.com");            //set up files to download            httpget httpget  = new httpget ("/test.zip");           // The absolute address of httpget can also be used directly here, of course, if it is not a specific addressDon't forget/end            //httpget httpget = new  httpget ("http://www.0431.la/");           // Httpresponse response = httpclient.execute (httpget);                       HttpResponse response  = httpclient.execute (httphost, httpget);            if (Httpstatus.sc_ok==response.getstatusline (). Getstatuscode ()) {                //Request Success                 //GET request Content                 httpentity entity = response.getentity ();                               //Display content                 if  (entity !=  NULL)  {                    //Here you can get the type of file   image/jpg /zip /tiff  and so on   but the discovery is not very effective, Sometimes the suffix is. rar But the null is taken, this special note                     system.out.println (Entity.getcontenttype ());                    //can determine whether the file data flow                     system.out.println ( Entity.isstreaming ());                    //to set locally saved files                   File  Storefile = new file ("C:/0431la.zip");                      fileoutputstream output = new  fileoutputstream (storefile);                    //Get network resources and write files                     inputstream input = entity.getcontent ();                    byte  b[] = new byte[1024];                    int j = 0;                    while (  (J = input.read (b))!=-1) {                         output.write (b,0,j);                    }                    output.flush ();                    output.close ();                 }                if  (entity != null)  {                    entity.consumecontent ();                }           }       }  } 
In fact, the code is very simple Ah, before a test to download the function, people said to get response on the download, I thought will automatically download to a place, a half-day or have to save their own files AH.


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.