I believe there is a misconception in your code. First you retrieve an item like this:
FileItemStream item = iterator.next();
Then, while reading the bytes of the file, you recover the InputStream
general request:
while ((read = req.getInputStream().read(bytes)) != -1) { ... }
However, the code should retrieve the InputStream
of the item, thus:
InputStream inputStream = item.openStream();
while ((read = inputStream.read(bytes)) != -1) { ... }
After all, the request could have more fields and files, it’s not even?
Anyway, I refactored a little the code you posted in the comments and I came to this:
if (ServletFileUpload.isMultipartContent(req)) {
FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
if (item.getName() != null) {
File uploadedFile = new File(path + "/" + item.getName());
byte[] bytes = new byte[2048];
int read = 0;
OutputStream outpuStream = new FileOutputStream(new File(uploadedFile.getAbsolutePath()));
InputStream inputStream = item.openStream();
while ((read = inputStream.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.close();
}
}
}
Cleiton, welcome to Stack Overflow. I think you need to elaborate a little better on your question. I don’t see how I can help but make innumerable conjectures. For example, you said the image is saved, but if you open it in an editor it is normal? Is the file the same size as the original? Are there any messages on the console? How is the code where you open Inputstream?
– utluiz
Thank you. Then the case and the following in the client the image appears but I need to take the image to record on the server, but the problem is that I can’t get the image on the server, I can get the image name, size, but the problem is to take for example the byte image and convert to image on the server... It became clearer my doubt ?
– Cleiton Migliorini
It helped a little, but understand that there are many different ways to recover a Java upload (Spring, Apache Commons File Upload, Primefaces, etc). It would be good for you to post the code of the method where you do this.
– utluiz
I’m using Commons, there goes the code
if (req.getHeader("Content-Type") != null
 && req.getHeader("Content-Type").startsWith(
 "multipart/form-data")) {
 ServletFileUpload upload = new ServletFileUpload();
 FileItemIterator iterator = upload.getItemIterator(req);
there I usewhile (iterator.hasNext()) { FileItemStream item = iterator.next();
was trying to get the image through the inputStream so :if (item.getName() != null) {

 File uploadedFile = new File(path + "/" + item.getName());
– Cleiton Migliorini
This is the part where I try to catch the inputStream and turn into an image
byte[] bytes = new byte[2048];

 OutputStream outpuStream = new FileOutputStream(
 new File(uploadedFile.getAbsolutePath()));

 System.out.println(uploadedFile.getAbsolutePath());
 int read = 0;

 System.out.println(req.getInputStream().toString());
 
 while ((read = req.getInputStream().read(bytes)) != -1) {
 
 outpuStream.write(bytes, 0, read);

} outpuStream.flush(); outpuStream.close();
– Cleiton Migliorini
@Please make an edit on your question to put the code of these comments. It is confusing to put as a comment. Then delete them. Here is different from a forum.
– Maniero