Java servlet: problem with corrupt file download

南楼画角 提交于 2019-12-04 20:51:42

The most common causes for randomly corrupted downloads from a servlet is that the servlet is not threadsafe and/or that it is reading bytes as characters. Sharing request or session based data among requests in the same session or servletcontext is also a possible cause for this problem.

You should not close the outputstream as it is managed by the servlet container. I'm not sure about the flush.

You have a serious flow in your code in below lines.

int length;
        while((length = input.read(buffer)) > 0)
        {
            output.write(buffer, 0, length);
        }

Your 'input' is a FileInputStream right? How can you make sure the FileInputStream has always more than 0 bytes available throughout your iteration? Instead above it must be written as below.

int length;
        while((length = input.read(buffer)) != -1)
        {
            output.write(buffer, 0, length);
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!