Why does my image look distorted?

I have Java code using a servlet and Apache Commons FileUpload to upload a file to a directory. It works great for character data (like text files), but image files look distorted. I can open them, but the image does not look as it should. Here is my code:

servlets

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String customerPath = "\\leetest\\"; // Check that we have a file upload request boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(); // Parse the request FileItemIterator iter = upload.getItemIterator(request); while (iter.hasNext()) { FileItemStream item = iter.next(); String name = item.getFieldName(); if (item.isFormField()) { // Form field. Ignore for now } else { BufferedInputStream stream = new BufferedInputStream(item .openStream()); if (stream == null) { LOGGER .error("Something went wrong with fetching the stream for field " + name); } byte[] bytes = StreamUtils.getBytes(stream); FileManager.createFile(customerPath, item.getName(), bytes); stream.close(); } } } } catch (Exception e) { throw new UploadException("An error occured during upload: " + e.getMessage()); } } 

StreamUtils.getBytes (stream) looks like this:

 public static byte[] getBytes(InputStream src, int buffsize) throws IOException { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); byte[] buff = new byte[buffsize]; while (true) { int nBytesRead = src.read(buff); if (nBytesRead < 0) { break; } byteStream.write(buff); } byte[] result = byteStream.toByteArray(); byteStream.close(); return result; } 

And finally, FileManager.createFile looks like this:

 public static void createFile(String customerPath, String filename, byte[] fileData) throws IOException { customerPath = getFullPath(customerPath + filename); File newFile = new File(customerPath); if (!newFile.getParentFile().exists()) { newFile.getParentFile().mkdirs(); } FileOutputStream outputStream = new FileOutputStream(newFile); outputStream.write(fileData); outputStream.close(); } 

Can anyone determine what I am doing wrong?

Cheers, Lee

+4
source share
5 answers

One thing that I don't like is here in this block from StreamUtils.getBytes ():

  1 while (true) { 2 int nBytesRead = src.read(buff); 3 if (nBytesRead < 0) { 4 break; 5 } 6 byteStream.write(buff); 7 } 

On line 6, it writes the entire buffer, regardless of how many bytes are being read. I am not sure that this will always be so. That would be more correct:

  1 while (true) { 2 int nBytesRead = src.read(buff); 3 if (nBytesRead < 0) { 4 break; 5 } else { 6 byteStream.write(buff, 0, nBytesRead); 7 } 8 } 

Note the "else" on line 5 along with two additional parameters (starting position of the index and length for copying) on ​​line 6.

I could assume that for large files, such as images, the buffer returns before it is full (maybe it expects more). This means that you will inadvertently write old data left at the end of the buffer. This almost certainly happens most of the time in EoF, assuming a buffer> 1 byte, but extra data in EoF is probably not the cause of your corruption ... it's just not desirable.

+4
source

I would just use commons io Then you could just do IOUtils.copy (InputStream, OutputStream);

He came up with many useful useful methods.

+1
source

Are you sure that the image does not go through a distorted image, or that you are not dropping some packets on the way.

0
source

I don’t know what the difference is, but there seems to be an inconsistency of method signatures. The getBytes() method, called in your doPost() method, has only one argument:

 byte[] bytes = StreamUtils.getBytes(stream); 

while the source of the method you included has two arguments:

 public static byte[] getBytes(InputStream src, int buffsize) 

Hope this helps.

0
source

Can you perform a checksum on the source file and the downloaded file and see if there are any immediate differences?

If there is, then you can look at the diff implementation to determine the exact part of the file that is missing.

Things that appear on the mind begin or end with a stream or end.

0
source

Source: https://habr.com/ru/post/1276533/


All Articles