首先,很抱歉,如果這個問題已經被問到,我已經看到了很多類似的問題,但沒有一個給我提供了一個有用的回答。在jsp中下載圖像:Illegalstateexception:getwriter()已經被調用了這個響應
我正在使用Struts,我想將圖像下載到img標記中。我在我的應用程序的其他地方使用它,並且正常工作,但是在其中一個動作中拋出IllegalStateException異常。這是一個登錄頁面,沒有什麼特別的。這是在動作的代碼,其中拋出異常:
public String mappingExecute(ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
OutputStream out = response.getOutputStream();
String idDocumento = request.getParameter("idDocumento");
FileResponse fResponse = WebConfig.getFileManager().downloadFile(idDocumento);
byte[] buffer = fResponse.getFileByteArray();
out.write(buffer);
out.close();
return null;
}
,這是例外:
java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:580)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
at net.bilbao.dxd.web.SrvDownloadFile.mappingExecute(SrvDownloadFile.java:22)
at net.bilbao.dxd.web.SrvAction.execute(SrvAction.java:56)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
(SrvDownloadFile是我的動作類的,異常是在response.getOutputStream()
拋出的名稱)
這裏是一個奇怪的部分:當我登錄時,下一個jsp調用相同的動作並且圖像顯示正常。當我回到登錄頁面時,圖像也顯示得很好!只有在您第一次進入登錄頁面時纔會拋出此異常。有什麼我可以做的,以檢查在我的行動稱之前調用getWriter()
?
對不起,我英語不好,並提前致謝。