问题
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