Httpclient uploads and downloads files

Source: Internet
Author: User

Use httpclient4.3 post to push files to the server
Preparation: httpclient-4.3.3.jar; httpcore-4.3.2.jar; httpmime-4.3.3.jar

Tags:<None>

Code snippet (1) [View all codes in full screen] 1. [Code] [Java] Code


/*** Upload File ** @ throws parseexception * @ throws ioexception */publicstaticvoidpostfile () throwsparseexception, ioexception {closeablehttpclient httpclient = httpclients. createdefault (); try {// path of the file to be uploaded string filepath = newstring ("F:/PIC/001.jpg "); // upload a common parameter and file to the following address: servlet httppost = newhttppost ("http: // localhost: 8080/XXX/xxx. action "); // convert a file into a stream object filebody file = newfile (filepa Th); filebody bin = newfilebody (File); stringbody uploadfilename = newstringbody ("change me to file name", contenttype. create ("text/plain", consts. utf_8); // run in browser-compatible mode to prevent garbled file names. Httpentity reqentity = multipartentitybuilder. create (). setmode (httpmultipartmode. browser_compatible ). addpart ("uploadfile", BIN) // uploadfile corresponds to the same name attribute of the server class <file type>. addpart ("uploadfilename", uploadfilename) // uploadfilename corresponds to the same name attribute of the server class <string type>. setcharset (charsetutils. get ("UTF-8 ")). build (); httppost. setentity (reqentity); system. out. println ("requested page address" + httppost. getrequestline (); // initiate a request and return the Request Response clo Seablehttpresponse response = httpclient.exe cute (httppost); try {system. out. println ("--------------------------------------"); // print the response status system. out. println (response. getstatusline (); // get the response object httpentity resentity = response. getentity (); If (resentity! = NULL) {// print the response length system. out. println ("response Content Length:" + resentity. getcontentlength (); // print the response content system. out. println (entityutils. tostring (resentity, charset. forname ("UTF-8");} // destroy entityutils. consume (resentity);} finally {response. close () ;}finally {httpclient. close () ;}/ *** download file * @ Param URL * @ Param destfilename xxx.jpg/xxx.png/xxx.txt * @ throws clientprotocolexception * @ THR Ows ioexception */publicstaticvoidgetfile (string URL, string destfilename) throwsclientprotocolexception, ioexception {// generate an httpclient object closeablehttpclient httpclient = httpclients. createdefault (); httpget = newhttpget (URL); httpresponse response = httpclient.exe cute (httpget); httpentity entity = response. getentity (); inputstream in = entity. getcontent (); file = newfile (destfilename ); Try {fileoutputstream fout = newfileoutputstream (File); Intl =-1; byte [] TMP = newbyte [1024]; while (L = in. Read (TMP ))! =-1) {fout. write (TMP, 0, L); // note that if outputstream is used here. write (buff), the image will be distorted, you can try} fout. flush (); fout. close () ;}finally {// close the lower-layer stream. In. Close () ;}httpclient. Close ();}


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.