Requirement: Use httpurlconnection to upload an image and transfer the image name to the previous one.
Simple Analysis: Write the image name + "|" separator + image stream in sequence when writing the stream
Then, the server receives the reprocessing stream and extracts the image name and image respectively.
/*** Upload method ** returns the uploaded file name ***/Public String upload (file F) {try {// server IP address (read from the property file) string hostip = filesupport. getserverip (); Url url = new URL ("http: //" + hostip + "/oxserver/receiveservlet"); httpurlconnection UC = (httpurlconnection) URL. openconnection (); // some parameters for image uploading are set to UC. setrequestproperty ("accept", "image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/vnd. MS-Excel, applica Tion/vnd. MS-PowerPoint, application/MSWord, application/X-Shockwave-flash, application/X-quickviewplus, */* "); UC. setrequestproperty ("Accept-language", "ZH-CN"); UC. setrequestproperty ("Content-Type", "multipart/form-data; boundary = --------------------------- 7d318fd100112"); UC. setrequestproperty ("Accept-encoding", "gzip, deflate"); UC. setrequestproperty ("User-Agent", "Mozilla/4.0 (compatible; MS IE 6.0; Windows NT 5.1) "); UC. setrequestproperty ("connection", "keep-alive"); UC. setdooutput (true); UC. setusecaches (true); // read the file stream int size = (INT) F. length (); byte [] DATA = new byte [size]; fileinputstream FD = new fileinputstream (f); outputstream out = UC. getoutputstream (); FCM. read (data, 0, size); // write the file name out. write (F. getname (). trim (). getbytes (); // write separator out. write ('|'); // write the image stream out. write (data ); Out. flush (); out. close (); FCM. close (); // read the response data int code = UC. getresponsecode (); string scurrentline = ""; // store the response result string stotalstring = ""; if (code = 200) {Java. io. inputstream is = UC. getinputstream (); bufferedreader reader = new bufferedreader (New inputstreamreader (is); While (scurrentline = reader. readline ())! = NULL) if (scurrentline. length ()> 0) stotalstring = stotalstring + scurrentline. trim ();} else {stotalstring = "remote server connection failed, error code:" + code;} return stotalstring;} catch (exception e) {e. printstacktrace ();} return NULL ;}
Server servlet:
Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {servletinputstream instream = request. getinputstream (); // get the int size of the HTTP request stream = request. getcontentlength (); // get the HTTP request stream Length byte [] buffer = new byte [size]; // cache the data read every time byte [] result = new byte [size]; // The array used to store the result int COUNT = 0; int rbyte = 0; // read the while (count <size) {rbyte = instream cyclically. read (buffer); // The actual read length is stored in rbyte sflj for (INT I = 0; I <rbyte; I ++) {result [count + I] = buffer [I];} count + = rbyte;} // first find the file name and the flag of the image stream '| 'int Index = 0; for (INT I = 0; I <result. length; I ++) {byte B = Result [I]; If (B = '|') {Index = I; break ;}} // store the file name byte name [] = new byte [index + 1]; // store the image byte [] IMG = new byte [size-Index]; for (INT I = 0; I <result. length; I ++) {if (I <index) {name [I] = Result [I];} if (I> index) {// note that the index of the IMG array starts from 0. IMG [I-index-1] = Result [I];} // restore the file name string filename = new string (name); instream. close (); string newfilename = renamefile (filename); // response client response. setcontenttype ("text/html"); // set response to respond to Chinese data. setcharacterencoding ("GBK"); printwriter out = response. getwriter (); // possible condition 0 database does not have relevant record 1 file name does not meet requirements other conditions are normal if (newfilename. equals ("0") {out. write ("0 |" + filename);} else if (newfilename. equals ("1") {out. write ("1 |" + filename);} else {out. write (filename);} Out. close (); // upload error abort subsequent operation if (newfilename. length () <= 1) {return;} file F = new file (imagesupport. getoriginal () + "/" + newfilename); fileoutputstream Fos = new fileoutputstream (f); FOS. write (IMG); FOS. flush (); FOS. close (); // change the image size and place it in the new location imagesupport. changeimagesize (F, imagesupport. get after () + "/" + newfilename, 300,300 );}
I wrote a program for uploading images in batches and it was tested.