File upload servlet gets message content from Httpservletrequest.getinputstream ()
Last Update:2016-06-15
Source: Internet
Author: User
<span id="Label3"></p><p><p>The previous time using spring thanks to the file upload, but with the mobile phone debugging, found that their plug-in can only use the form of native data stream upload files, according to the specification of HTTP to see the message of the upload file, write the following method, for everyone to Use:</p></p> <table> <tbody> <tr> <td valign="top" width="714">@Note (Note = "bigant:servlet upload file", author = "zhangwenbo")<br>@RequestMapping ("modifyPictureByStream2")<br>@ResponseBody<br>Public simplemessage<object> modifyPictureByStream2 (httpservletrequest req, httpservletresponse Response) {<br><br>Logger.info ("modifyheaderpicture have invoked.filename is");<br><br>simplemessage<object> sm = new Simplemessage<object> ();<br><br>BufferedReader Bufferreader = null;<br><br>Bytearrayoutputstream OutputStream = null;<br><br>FileOutputStream fileout = null;<br><br>try {<br>String boundary =<br>Req.getcontenttype (). substring (req.getcontenttype (). indexOf ("boundary=")<br>+ 9);<br><br>Getting messages from the stream<br>Bufferreader = new BufferedReader (new inputstreamreader (req.getinputstream ()));<br><br>OutputStream = new Bytearrayoutputstream ();<br><br>String line = Bufferreader.readline ();<br><br>Boolean contentbegin = false;<br><br>String filename = "";<br><br>While (true) {<br><br>If (null! = line &&! "). equals (line.trim ())) {<br>int filename_index = Line.indexof ("filename=\" ");<br><br>If (filename_index >-1) {<br><br>Contentbegin = True;//ready to start reading file contents<br><br>filename = line.substring (filename_index + ten, line.length ()-1);<br><br>Logger.info ("filename is" + filename);<br><br>Skip two lines and go directly to the contents of the file<br>line = Bufferreader.readline ();<br><br>line = Bufferreader.readline ();<br>}<br>Logger.info ("filename_index:" + filename_index);<br>} else {<br>If (contentbegin) {<br>Break;//jump out of the loop<br>}<br>}<br><br>If (contentbegin) {<br>Logger.info ("line was:" + line);<br><br>Outputstream.write (line.getbytes ());<br>}<br><br>Logger.info ("read Next line");<br><br>line = Bufferreader.readline ();<br>}<br><br>int filename_split = Filename.indexof (".");<br><br>Create a temporary file locally<br>File FileIO = file.createtempfile (filename.substring (0, filename_split), filename.substring (filename_split));<br><br>If (!fileio.exists ()) {<br>Fileio.createnewfile ();<br>}<br><br>Write a file<br>Fileout = new FileOutputStream (fileIO);<br><br>Fileout.write (outputstream.tobytearray ());<br><br>Fileout.flush ();<br><br>map<string, object> postbody = new hashmap<string, object> ();<br><br>Postbody.put ("loginname", req.getsession (). getattribute (biganthelper.session_user_id));<br><br>Send file<br>Bigantbaseres Uploadresult = bigantclient.getinstance (). uploadpicture (postbody, filename, fileIO);<br><br>Logger.info ("upload return msg is" + uploadresult);<br><br>If (null! = Uploadresult) {<br>If ("1". equals (uploadresult.getstatus (). trim ())) {<br>Sm.set ("data", Uploadresult.getdata ());<br>} else {<br>Sm.set ("message", uploadresult.getmsg ());<br>}<br>} else {<br>Sm.set ("message", "upload fail");<br>}<br><br>Delete temporary files regardless of success or failure<br>If (fileio.exists ()) {<br>Fileio.delete ();<br>}<br><br>} catch (ioexception E) {<br>Logger.info ("upload file fail.", e);<br>} finally {<br>try {<br>If (null! = bufferreader) {<br>Bufferreader.close ();<br>}<br><br>If (null! = Outputstream) {<br>Outputstream.close ();<br>}<br><br>If (null! = fileout) {<br>Fileout.close ();<br>}<br>} catch (Exception E2) {<br>Logger.info ("close InputStream fail.", e2);<br>}<br>}<br>Return sm;<br>}</td> </tr> </tbody> </table><p><p>For reference only!<br></p></p><p><p>This article is from the "java" blog, so be sure to keep this source http://zwbjava.blog.51cto.com/2789897/1789242</p></p><p><p>File upload servlet gets message content from Httpservletrequest.getinputstream ()</p></p></span>