在Servlet類的doPost方法執行此操作時對PrintWriter的得到一個IOExcepiton:PrintWriter的產生java.io.IOException:關閉
doPost(HttpServletRequest request,
HttpServletResponse response){
String replyMessage = "STATUS_ACCEPT";
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/plain; charset=utf-8");
response.setContentLength(replyMessage.length());
response.setIntHeader("content-length", replyMessage.length());
response.getWriter().println(replyMessage);//getting IOException closed on calling this method
response.flushBuffer();}
如果我使用的OutputStream發送數據沒有錯誤:
ServletOutputStream outputStream = response.getOutputStream();
byte[] result = "STATUS_ACCEPT".getBytes("UTF-8");
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/plain; charset=utf-8");
response.setContentLength(result.length);
response.setIntHeader("content-length", result.length);
outputStream.write(httpOutData);
我應該如何使用printwriter沒有得到IOException?
如果內容長度是自動計算的,我們應該什麼時候使用setContentLength()方法。 – vjk
根據Servlet容器的設置,_may_的長度會阻止Servlet容器中的緩衝_large_輸出。 –