Public Static voidMain (string[] args) {closeablehttpclient httpclient=Httpclients.createdefault (); Closeablehttpresponse Response=NULL; String result=NULL; InputStream InputStream=NULL; Try{InputStream=NewFileInputStream ("D:\\test.pdf"); HttpPost HttpPost=NewHttpPost ("Http://127.0.0.1/test"); Multipartentitybuilder Builder=multipartentitybuilder.create (); Builder.setmode (httpmultipartmode.browser_compatible); Builder.addbinarybody ("WJ", InputStream, Contenttype.create ("Multipart/form-data"), "Test.pdf"); //Build request Parameters normal form ItemStringbody Stringbody =NewStringbody ("100001", Contenttype.multipart_form_data); Builder.addpart ("Serial_no", stringbody);Stringbody=NewStringbody ("200", Contenttype.multipart_form_data); Builder.addpart ("XPosition", Stringbody); Stringbody=NewStringbody ("200", Contenttype.multipart_form_data); Builder.addpart ("YPosition", Stringbody); Stringbody=NewStringbody ("2", Contenttype.multipart_form_data); Builder.addpart ("Sealtype", Stringbody); Stringbody=NewStringbody ("1", Contenttype.multipart_form_data); Builder.addpart ("Paegnum", Stringbody); Builder.setcharset (Charsetutils.get ("UTF-8")); Httpentity Entity=Builder.build (); Httppost.setentity (entity); Response=Httpclient.execute (HttpPost); intStatusCode =response.getstatusline (). Getstatuscode (); System.out.println ("StatusCode:" +StatusCode); if(StatusCode = =HTTPSTATUS.SC_OK) {httpentity resentity=response.getentity (); Result= Entityutils.tostring (resentity, "UTF-8"); } System.out.println ("Result:" +result); } Catch(IOException ex) {Logger.getlogger (qztest.class. GetName ()). log (Level.severe,NULL, ex); } finally { Try { if(NULL!=InputStream) {Inputstream.close (); } } Catch(IOException ex) {Logger.getlogger (qztest.class. GetName ()). log (Level.severe,NULL, ex); } finally{Httpclient.getconnectionmanager (). Shutdown (); } } }
Dependent Packages:
Commons-logging-1.2.jar
Httpclient-4.5.3.jar
Httpcore-4.4.6.jar
Httpmime-4.5.3.jar
Simulate form file upload via HttpClient4.5