struts2 java.lang.IllegalStateException: getWriter() has already been called for this response(异常解决)
2017-03-16 14:36
555 查看
预告: 重点请看最后面。
做excel导出时遇到的问题,很多时候会遇到此错误,异常如下:
java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:628)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:196)
at com.wrt.action.order.EndOrderAction.exportInfo(EndOrderAction.java:143)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:446)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:285)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
背景如下:
使用Struts2框架,点击某个按钮实现列表excel导出
2.大致代码如下
最终解决方案,将response.reset(); 放到OutputStream output = getRepsonse().getOutputStream();前面,
运行效果如下成功。
.
原因,避免可能出现未关闭的getWriter(),使用了response.reset();进行刷新,而此时却在未刷新的情况下进行了OutputStream output = getRepsonse().getOutputStream();操作..... 简之:就是位置放错了
.
会出现如下异常的三种可能:
1.通过response.reset(); 刷新可能存在一些未关闭的getWriter(). 来源:http://blog.csdn.net/wonder4/article/details/8476603
2.struts2请求方法中返回了success此类会返回数据或者界面的,举例如下:
public String saveSubmitOrder(){
return "success";
}
改成
public String saveSubmitOrder(){
return null;
}
或者void。
3.存在多个getWriter且未关闭状态,或者getWriter与OutputStream同时出现。
总之,一个请求过来只会有一个返回结果,如果存在两个或以上则会出现此异常.
做excel导出时遇到的问题,很多时候会遇到此错误,异常如下:
java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:628)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:196)
at com.wrt.action.order.EndOrderAction.exportInfo(EndOrderAction.java:143)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:446)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:285)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
背景如下:
使用Struts2框架,点击某个按钮实现列表excel导出
2.大致代码如下
public void exportInfo(){ try { //web对象 HSSFWorkbook wb = new HSSFWorkbook(); //创建表头等 HSSFSheet sheet = wb.createSheet("test1"); //创建行 HSSFRow row = sheet.createRow(0); //创建列 HSSFCell cell = row.createCell(0); //**************省略excel创建部分.. HttpServletResponse response = ServletActionContext.getResponse(); OutputStream output = getRepsonse().getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename=details.xls"); response.setContentType("application/msexcel"); wb.write(output); output.close(); System.out.println("成功创建excel文件"); } catch (Exception e) { e.printStackTrace(); } }
最终解决方案,将response.reset(); 放到OutputStream output = getRepsonse().getOutputStream();前面,
HttpServletResponse response = ServletActionContext.getResponse(); response.reset(); OutputStream output = getRepsonse().getOutputStream(); response.setHeader("Content-disposition", "attachment; filename=details.xls"); response.setContentType("application/msexcel");
运行效果如下成功。
.
原因,避免可能出现未关闭的getWriter(),使用了response.reset();进行刷新,而此时却在未刷新的情况下进行了OutputStream output = getRepsonse().getOutputStream();操作..... 简之:就是位置放错了
.
会出现如下异常的三种可能:
1.通过response.reset(); 刷新可能存在一些未关闭的getWriter(). 来源:http://blog.csdn.net/wonder4/article/details/8476603
2.struts2请求方法中返回了success此类会返回数据或者界面的,举例如下:
public String saveSubmitOrder(){
return "success";
}
改成
public String saveSubmitOrder(){
return null;
}
或者void。
3.存在多个getWriter且未关闭状态,或者getWriter与OutputStream同时出现。
总之,一个请求过来只会有一个返回结果,如果存在两个或以上则会出现此异常.
相关文章推荐
- java.lang.IllegalStateException: getWriter() has already been called for this response问题解决
- java.lang.IllegalStateException: getWriter() has already been called for this response问题解决
- java.lang.IllegalStateException: getWriter() has already been called for this response问题解决
- java.lang.IllegalStateException: getWriter() has already been called for this response问题解决
- java.lang.IllegalStateException: getWriter() has already been called for this response 解决办法
- java.lang.IllegalStateException: getOutputStream() has already been called for this response 分类: 异常
- java.lang.IllegalStateException: getWriter() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response解决方法及解释
- SSH中发生异常java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException:getOutputStream() has already been called for this response的解决方法
- java.lang.IllegalStateException: getOutputStream() has already been called for this response的解决办法
- java.lang.IllegalStateException: getWriter() has already been called for this response
- java.lang.IllegalStateException: getWriter() has already been called for this response
- java.lang.IllegalStateException: getWriter() has already been called for this response
- 异常处理---java.lang.IllegalStateException: getWriter() has already been called for this
- 解决java.lang.IllegalStateException: getOutputStream() has already been called for this response
- struts2:java.lang.IllegalStateException: getOutputStream() has already been called for this response
- 验证码异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getWriter() has already been called for this response
- java.lang.IllegalStateException: getInputStream() has already been called for this request该异常的解决方法!