java - InputStream Infinit Loop -
i'm trying upload file android tomcat server running jersey. i'm packaging inside post request.
this in android:
protected string doinbackground(string... params) { string url = params[0]; string pathtofile = params[1]; httpclient httpclient = new defaulthttpclient(); httpcontext localcontext = new basichttpcontext(); httppost httppost = new httppost(); httpresponse response = null; httppost.addheader("cookie", "sessiontoken=~session"); try { httppost.seturi(new uri(url)); httppost.setheader("accept", "application/json"); multipartentity entity = new multipartentity( httpmultipartmode.browser_compatible); file file = new file(pathtofile); if(!file.exists()) return null; filebody filebody = new filebody(file); entity.addpart("file", filebody); httppost.setentity(entity); response = httpclient.execute(httppost); httpentity result = response.getentity(); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (urisyntaxexception e) { // todo auto-generated catch block e.printstacktrace(); } catch(exception e){ e.printstacktrace(); } return response.getstatusline().tostring();
in server have following:
i receive inputstream of type "org.jvnet.mimepull.datahead$readmultistream" , while reading it, read goes 1024 after reaching end of file.
@post @consumes({ mediatype.multipart_form_data }) @produces({ mediatype.application_json }) public response uploadstoragefile(@context uriinfo ui, @context httpheaders hh, @formdataparam("file") inputstream uploadedinputstream, @formdataparam("file") formdatacontentdisposition filedetail){ system.out.println(uploadedinputstream.getclass().getname()); string uploadedfilelocation = filedetail.getfilename(); long size = filedetail.getsize(); // save writetofile(uploadedinputstream); string output = "file uploaded : "; return response.status(200).entity(output).build(); } private void writetofile(inputstream uploadedinputstream) { try { outputstream out = new fileoutputstream(new file("test.jpg")); int read = 0; byte[] bytes = new byte[1024]; while ((read = uploadedinputstream.read(bytes)) > 0) { out.write(bytes, 0, read); } out.flush(); out.close(); } catch (ioexception e) { e.printstacktrace(); } }
if length of file 8192, cycle goes this: 1024,2048,3072,4096,5120,6144,7168,8192,1024 -> why?
note: have tried condition @ -1.
can figure out going on?
since helped solve problem, latest comment indicates this bug responsible.
both methods "read" on readmultistream class violate contract of implemented interface java.io.inputstream. javadoc states, -1 should returned @ end of stream, readmultistream class returns -1 first call (at end of stream), subsequent calls throw exception instead. problem simulated sending stream of characters on web service , wrapping java.io.bufferedreader on client side. when stream doesn't end new line character, usual usage of method readline fails.
fix version/s: 2.2.6
Comments
Post a Comment