IOUtils.copy fails 2nd time

偶尔善良 提交于 2020-01-06 07:57:05

问题


I have a servlet that uses a method with the following logic:

        ServletOutputStream out = response.getOutputStream();
        IOUtils.copy(content, out);
        out.close();

When I run through it the first time, everything works as expected. If I attempt to do the same procedure a second time, with the same content, out is empty. I have inspected the content object while debugging the second time and it's identical to the content the first time. Any ideas what could be happening?


回答1:


Assuming that you are not trying to write to a closed output stream (which was my first guess, but your comment leads me to think that the servlet itself is re-invoked, which will open a new response), then it's likely that your input stream needs to be reset. Depending on what kind of actual stream it is, you can either use mark and reset on the stream itself, or you can recreate the stream, or you can copy it into memory as a byte array for reuse. See this question: Read stream twice



来源:https://stackoverflow.com/questions/14103864/ioutils-copy-fails-2nd-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!