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 ();} |