1
I have a Webservice that saves the photo in a folder of the server that was sent by the Android application through the following code:
...
int read = 0;
byte[] bytes = new byte[1024];
OutputStream out = new FileOutputStream(new File( caminhodestinofotos + uploadedFileName ));
while (( read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
...
So far so good, but we decided to save this photo in the database.
To that end I thought of transforming the Inputstream that arrives the function of the Webservice in an array of bytes.
I added the following line at the beginning of the function:
byte[] arrbytes = sun.misc.IOUtils.readFully(uploadedInputStream, -1, true);
It turns out that after running this line the LOOP that reads from the file adds to the byte array does not happen.
The line read = uploadedInputStream.read(bytes)) != -1
returns -1 as if Inputstream is at the end of the file and a sequential read cannot occur again.
How should I proceed? How to reset and place the reading pointer at the beginning of file again, or what would be another way to use that same Inputstream to save to file and save to database?
Grateful.
Hello, in Inputstream there are two methods for this: the
mark(int readLimit)
and thereset()
. Themark
does what the name says, marks a point on the file. Thereset()
resets to exactly this point. Try using amark(1000)
before the line you want to read in loop, and at the end give thereset()
. I don’t know exactly if this is what I was looking for, so I didn’t put it in answer.– Memphys
The same error occurs: java.io.Ioexception: mark/reset not supported
– Reginaldo Rigo
You can edit your post with the complete code of this method?
– Memphys
The inputStream that arrived at the function did not support dialing and resetting. Copy to another one that supports it : Bufferedinputstream bi = new Bufferedinputstream(uploadedInputStream); and I worked with this one. I marked copied to bytes[] array, reset and saved in table. Offer the answer so that I can accept it.
– Reginaldo Rigo
Okay, I’m glad you settled!
– Memphys