Using ServletOutputStream to write very large files in a Java servlet without memory issues

后端 未结 10 1741
太阳男子
太阳男子 2020-12-01 00:28

I am using IBM Websphere Application Server v6 and Java 1.4 and am trying to write large CSV files to the ServletOutputStream for a user to download. Files are

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 00:41

    your code has an infinite loop.

    do
    {
        bytesRead = inputStream.read(buffer, offset, buffer.length);
        resp.getOutputStream().write(buffer, 0, bytesRead);
    }
    while (bytesRead == buffer.length);
    

    offset has the same value thoughout the loop, so if initially offset = 0, it will remain so in every iteration which will cause infinite-loop and which will leads to OOM error.

提交回复
热议问题