Need to do a httpclient upload, and then snapped online for information
1. First used in the system before the Commons-httpclient upload, find the information after a meal change, and then test
Postmethod filepost = new Postmethod (URL); Filepost.setparameter ("System", "Vinuxpost"); try {part part[] = Uploadrequesthelper.getpart (request); Filepost.setrequestentity (New Multipartrequestentity (part, Filepost.getparams ())); HttpClient client = new HttpClient (); Client.gethttpconnectionmanager (). Getparams (). Setconnectiontimeout (n); int Status = Client.executemethod (FilePost); SYSTEM.OUT.PRINTLN (status);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}
One of the main code, upload the file way with a byte, and then take the required parameters, and then encountered a strange problem, that is, the parameter is not over, and then removed the upload of the file part, the parameters came over, it is strange, with an afternoon, the Internet to find information or no solution, there is no solution!!! I hope someone can tell me, hahaha, maybe it is the jar package version of the problem, I see a lot of information on the Internet can be with parameters to upload files.
I hope the great God can help me solve the above problems.
2. Then Apache httpclient upload, on the code
Closeablehttpclient httpClient = Httpclients.createdefault (); HttpPost HttpPost = new HttpPost (URL); list<multipartfile> listfile = uploadrequesthelper.getlistmultipartfile (request);//Create the form field content text to be processed stringbody system = new Stringbody ("Vinuxpost"); Multipartentity reqentity = new multipartentity (); for (Multipartfile file:listfile) {bytearraybody bab = new ByteArrayBo Dy (File.getbytes (), File.getname ()) Reqentity.addpart (File.getname (), Bab);} Reqentity.addpart ("System", System); httppost.setentity (reqentity); Closeablehttpresponse response = Httpclient.execute (HttpPost); System.out.println ("Status:" + Response.getstatusline ());
No accident, success, you can continue the following work, hahaha
Summary of the differences between Commons-httpclient and Apache HttpClient:
First of all, commons-httpclient seems to be rarely updated, and the document is not very sufficient, online examples of many are repeated, Apache HttpClient has been updated, haha, more information!!!
Stick Apache httpclient maven Hey, here it is.
<Dependency> <groupId>Org.apache.httpcomponents</groupId> <Artifactid>HttpClient</Artifactid> <version>4.3.3</version> </Dependency> <Dependency> <groupId>Org.apache.httpcomponents</groupId> <Artifactid>Httpmime</Artifactid> <version>4.2.1</version> </Dependency>
Need Httpmime, don't forget.
On httpclient upload with parameters "commons-httpclient and Apache httpclient differences"