Httpclient 4.3 psot method solves the problem of garbled Chinese Characters in file upload and Parameter

Source: Internet
Author: User

Not much nonsense. There is a code directly!

1 package httpclient; 2 3 Import Java. io. file; 4 Import Java. NIO. charset. charset; 5 6 Import Org. apache. HTTP. consts; 7 Import Org. apache. HTTP. header; 8 Import Org. apache. HTTP. httpentity; 9 Import Org. apache. HTTP. client. methods. closeablehttpresponse; 10 Import Org. apache. HTTP. client. methods. httppost; 11 import Org. apache. HTTP. entity. contenttype; 12 Import Org. apache. HTTP. entity. mime. httpmultipartmode; 1 3 Import Org. apache. HTTP. entity. mime. multipartentitybuilder; 14 Import Org. apache. HTTP. entity. mime. content. filebody; 15 Import Org. apache. HTTP. entity. mime. content. stringbody; 16 Import Org. apache. HTTP. impl. client. closeablehttpclient; 17 import Org. apache. HTTP. impl. client. httpclients; 18 Import Org. apache. HTTP. util. charsetutils; 19 Import Org. apache. HTTP. util. entityutils; 20 21 public class httpdemo {22 p Ublic static void main (string [] ARGs) throws exception {23 closeablehttpclient httpclient = httpclients. createdefault (); 24 try {25 httppost = new httppost ("http: // localhost: 28080" 26 + "/PM/restful/trip/saveptriprecord "); 27 28 // filebody bin = // new filebody (new file ("D: // tree .jpg"); 29 file = new file ("D: // tree .jpg "); 30 filebody bin = new filebody (File); 31 // plain text userid = New stringbody (33 "f39d2a3e466b292b01466f2388900020", contenttype. create (34 "text/plain", consts. utf_8); 35 stringbody startdate = new stringbody ("2014-8-1 09:13:12", 36 contenttype. create ("text/plain", consts. utf_8); 37 stringbody enddate = new stringbody ("2014-8-1 09:13:12", 38 contenttype. create ("text/plain", consts. utf_8); 39 stringbody triptime = new stringbody ("6.8", contenttype. create (40 "te XT/plain ", consts. utf_8); 41 stringbody tripaddress = new stringbody ("what", contenttype. create (42 "text/plain", consts. utf_8); 43 stringbody tripresons = new stringbody ("what", contenttype. create (44 "text/plain", consts. utf_8); 45 httpentity reqentity = multipartentitybuilder. create () 46. setmode (httpmultipartmode. browser_compatible) // sets it to compatible with the memory generator mode 47. setcharset (charsetutils. get ("utf8") // set limit to 48. addpa RT ("userid", userid ). addpart ("startdate", startdate) 49. addpart ("enddate", enddate ). addpart ("triptime", triptime) 50. addpart ("tripaddress", tripaddress) 51. addpart ("tripresons", tripresons ). addpart ("bin", BIN) 52. build (); 53 54 httppost. setentity (reqentity); 55 system. out56. println ("executing request" + httppost. getrequestline (); 57 closeablehttpresponse response = httpclient.exe cute (httppost); 58 Try {59 system. out. println ("--------------------------------------"); 60 system. out. println (response. getstatusline (); 61 httpentity resentity = response. getentity (); 62 if (resentity! = NULL) {63 system. out. println ("response Content Length:" 64 + resentity. getcontentlength (); 65} 66 system. out. println (entityutils. tostring (resentity, 67 charset. forname ("UTF-8"); 68 entityutils. consume (resentity); 69} finally {70 response. close (); 71} 72} finally {73 httpclient. close (); 74} 75} 76 77}

 

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.