In the previous project, parameters and files are sent using Java imitation http post. You can use urlconnection to process parameters or files.
However, the project involves both passing common parameters and multiple files (not simply passing XML files ). After searching on the internet, I found that I was using httclient for response operations. At first I tried multiple times and still could not pass parameters and files. Later I found that when I used httpclient, I could not use request. getparameter () is used to obtain common parameters, but upload is used on the server.
Httpclient4.2 jar download: http://download.csdn.net/detail/just_szl/4370574
Client code:
Import Java. io. bytearrayoutputstream; import Java. io. file; import Java. io. ioexception; import Java. io. inputstream; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. httpstatus; import Org. apache. HTTP. parseexception; import Org. apache. HTTP. client. httpclient; import Org. apache. HTTP. client. methods. httppost; import Org. apache. HTTP. entity. mime. multipartentity; import Org. apache. HTTP. entity. mime. content. filebody; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. util. entityutils;/**** @ author <a href = "mailto: just_szl@hotmail.com"> geray </a> * @ Version 1.0, 2012-6-12 */public class httppostargumenttest2 {// file1 and file2 are in the same folder. filepath is the path specified by the folder public void submitpost (string URL, string filename1, string filename2, string filepath) {httpclient = new defaulthttpclient (); try {httppost = new httppost (URL); filebody bin = new filebody (new file (filepath + file. separator + filename1); filebody bin2 = new filebody (new file (filepath + file. separator + filename2); stringbody comment = new stringbody (filename1); multipartentity reqentity = new multipartentity (); reqentity. addpart ("file1", BIN); // file1 is the file upload in the request background; Attribute reqentity. addpart ("file2", bin2); // file2 is the file upload in the request background; Attribute reqentity. addpart ("filename1", comment); // filename1 is a common parameter in the request background; Attribute httppost. setentity (reqentity); httpresponse response = httpclient.exe cute (httppost); int statuscode = response. getstatusline (). getstatuscode (); If (statuscode = httpstatus. SC _ OK) {system. out. println ("normal server response ..... "); httpentity resentity = response. getentity (); system. out. println (entityutils. tostring (resentity); // The tool class provided by httpclient reads the returned data system. out. println (resentity. getcontent (); entityutils. consume (resentity) ;}} catch (parseexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {try {httpclient. getconnectionmanager (). shutdown ();} catch (exception ignore) {}}/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubhttppostargumenttest2 httppostargumenttest2 = new httppostargumenttest2 (); encrypt ("http: // 127.0.0.1: 8080/demo/receivedata. do "," test. XML "," test.zip "," d: // test ");}}
Server code:
Public void receivedata (httpservletrequest request, httpservletresponse response) throws appexception {printwriter out = NULL; response. setcontenttype ("text/html; charset = UTF-8"); map = new hashmap (); fileitemfactory factory = new diskfileitemfactory (); servletfileupload upload = new servletfileupload (factory ); file directory = NULL; List <fileitem> items = new arraylist (); try {items = upload. parserequ Est (request); // obtain all the files iterator <fileitem> it = items. iterator (); While (it. hasnext () {fileitem fitem = (fileitem) it. next (); string fname = ""; object fvalue = NULL; If (fitem. isformfield () {// value of the common text box fname = fitem. getfieldname (); // fvalue = fitem. getstring (); fvalue = fitem. getstring ("UTF-8"); map. put (fname, fvalue);} else {// get the value of the uploaded file fname = fitem. getfieldname (); fvalue = fitem. getinputstream (); map. Put (fname, fvalue); string name = fitem. getname (); If (name! = NULL &&! ("". Equals (name) {name = Name. substring (name. lastindexof (file. separator) + 1); // string stamp = stringutils. getformattedcurrdatenumberstring (); string timestamp_str = timeutils. getcurryearyyyy (); directory = new file ("D: // test"); directory. mkdirs (); string filepath = ("D: // test") + timestamp_str + file. separator + name; map. put (fname + "filepath", filepath); inputstream is = fitem. getinputstream (); fileo Utputstream Fos = new fileoutputstream (filepath); byte [] buffer = new byte [1024]; while (is. read (buffer)> 0) {FOS. write (buffer, 0, buffer. length);} FOS. flush (); FOS. close (); map. put (fname + "FILENAME", name) ;}}} catch (exception e) {system. out. println ("An error occurred while reading the HTTP request property value! "); // E. printstacktrace (); logger. error ("An error occurred while reading the HTTP request property value");} // data processing try {out = response. getwriter (); out. print ("{success: True, MSG: 'Received successfully'}"); out. close ();} catch (ioexception e) {e. printstacktrace ();}}
Http://blog.csdn.net/Just_szl/article/details/7659347