Explicit image image of ok image on windows but changed on linux

I am loading an image from a url like this:

private void download(String srcUrl, String destination) throws Throwable { File file = new File(destination); if (!file.exists()) { file.createNewFile(); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); BufferedInputStream in = new BufferedInputStream(new URL(srcUrl).openStream()); byte bytes[] = new byte[1024]; while (0 <= in.read(bytes, 0, 1024)) { out.write(bytes); } out.close(); in.close(); } } 

In the windows, the resulting image is an ideal copy of the original. However, on my debian server, the image changes: the lower right area of ​​the image is blurred. This happens in every picture, and it is always in the same area of ​​the image.

Thanks for the help!

+4
source share
1 answer

I do not know why the result is different from systems, although the code is erroneous, and I suspect that it has something to do with the observed behavior.

 while (0 <= in.read(bytes, 0, 1024)) { out.write(bytes); } 

Must be:

 int count; while ((count = in.read(bytes, 0, 1024)) > 0) { out.write(bytes, 0, count); } 

Otherwise, there is a [high] chance that β€œgarbage” is written at the end, which may explain the blur, depending on the program that is trying to view the [damaged] image file. (The size of the array used as a buffer does not change - it should record only as much data as was written on it.)

Happy coding.

+5
source

All Articles