Hi, i'm having some trouble sending a byte array via http connection stream to servlet, here is my client code:
and the servlet code:
The servlet reads 4 chunks (2048 bytes) and then stops, and ofcourse the image cannot be read, i get an EOFException. I tried forcing the servlet to read byte[167096] (167096 is bytes.length on the client) and the image gets created but only the first ~3% of the pixels are shown, the rest is just white.
Maybe someone can tell me what i'm doing wrong ? i would appreciate it.
Thanks,
Alex
I suggest you take a look at ByteArrayOutputStream. It will make the problem as easy as simple InputStream-to-OutputStream copying after which you call toByteArray() on the ByteArrayOutputStream to get the byte array.
However, in your case I see that you are first gathering all data in a byte[], then save that byte[] to disk. Why not use a direct FileOutputStream to copy the contents of the InputStream to instead of saving the intermediate result?
Rob Prime wrote:I suggest you take a look at ByteArrayOutputStream. It will make the problem as easy as simple InputStream-to-OutputStream copying after which you call toByteArray() on the ByteArrayOutputStream to get the byte array.
I don't really understand what do you mean by this, isn't my code working the way it is ?
Rob Prime wrote:However, in your case I see that you are first gathering all data in a byte[], then save that byte[] to disk. Why not use a direct FileOutputStream to copy the contents of the InputStream to instead of saving the intermediate result?
I'm not really interested in saving a file to disk, i just did that so i can check if the image came out ok.
That should work if you change the "> 0" on line 6 of the servlet code to ">= 0". If read returns 0 that does not mean there is no more data, only that there is no data right now. A return value of -1 is used to indicate there is no data and there won't be any data anymore.