java - How to get BufferedInputStream from Multipart? -
i'm trying bufferedinputstream uploaded cvs file.
i'm working multipart derived cvs file.
when first multipart, it's bufferedinputstream, buffer null.
but if deeper down, there's buffer in coyoteinputstream , has data.
how can @ second buffer? code below.
and of course it's throwing null exception when gets
while ((multipartdatapos = stream.read(buffer)) >= 0)
what doing wrong? mistaken coyoteinputstream data want?
public byte[] handleuploadedfile(multipart multipart) throws eofexception {
multipart multipartdata = null; bufferedinputstream stream = null; int basicbuffersize = 0x2000; byte[] buffer = new byte[basicbuffersize]; int bufferpos = 0; try { while (multipart.hasnext()) { int multipartdatapos = bufferpos; multipartdata = (multipart) multipart.next(); stream = new bufferedinputstream(multipartdata.getinputstream()); while ((multipartdatapos = stream.read(buffer)) >= 0) { int len = stream.read(buffer, multipartdatapos, buffer.length - multipartdatapos); multipartdatapos += len; } bufferpos = bufferpos + multipartdatapos; } } ...
your code doesn't make sense.
while ((multipartdatapos = stream.read(buffer)) >= 0) {
at point have read multipartdatapos
bytes buffer
, buffer[0..multipartdatapos-1]
contains data read.
int len = stream.read(buffer, multipartdatapos, buffer.length - multipartdatapos);
at point doing read, return -1, otherwise add data multipartpos
multipartdatapos+len-
.
multipartdatapos += len;
this step valid if len > 0
.
and doing nothing buffer; , next time around loop clobber whatever read.
the correct way read stream in java follows:
while ((count = in.read(buffer)) > 0) { // use buffer[9..count-1], example out.write(buffer, 0, count); }
i don't understand why think access underlying stream required or it's going give you don't have.
Comments
Post a Comment