`

getOutputStream() has already been called for this response

阅读更多
错误信息:
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:626)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:205)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:182)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:123)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
at org.apache.jsp.error_jsp._jspService(error_jsp.java:89)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:581)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:518)
at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:166)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275)
at com.ipharmacare.plat.interceptor.DeptAndDocInterceptor.intercept(DeptAndDocInterceptor.java:66)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.ipharmacare.plat.interceptor.TrimStringInterceptor.doIntercept(TrimStringInterceptor.java:43)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.ipharmacare.plat.interceptor.RequestTypeInterceptor.intercept(RequestTypeInterceptor.java:27)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.ipharmacare.plat.interceptor.BaseUrlInterceptor.intercept(BaseUrlInterceptor.java:43)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.ipharmacare.xiaojdm.filter.DBUpdatingFilter.doFilter(DBUpdatingFilter.java:60)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.dj.core.ConFilter.doFilter(ConFilter.java:34)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:350)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.ipharmacare.xiaojdm.filter.SpringFilter.doFilterInternal(SpringFilter.java:34)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:550)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)...


原先代码:
	/**
	 * 加载图片
	 * 
	 * @return
	 */
	@Action(value = "inputStream", results = { @Result(name = "stream", type = "stream", params = {
			"contentType", "image/jpeg", "inputName", "inputStream" }) })
	public String inputStream() {

		CustomInstructionImg cii = customInstructionImgService
				.get(getParameter("id"));
		byte[] imgFile = cii.getImgFile();

		if (null != cii && null != imgFile) {
			InputStream in = null;
			try {
				in = new ByteArrayInputStream(imgFile);
				inputStream = new BufferedInputStream(in);
			} finally {
				IOUtils.closeQuietly(inputStream);
				IOUtils.closeQuietly(in);
			}
		}
		return "stream";
	}

	private BufferedInputStream inputStream;省略get/set


错误原因是不能使用BufferedInputStream作为输出对象,应该用ByteArrayInputStream
修改后的代码如下:
	/**
	 * 加载图片
	 * 
	 * @return
	 */
	@Action(value = "inputStream", results = { @Result(name = "stream", type = "stream", params = {
			"contentType", "image/jpeg", "inputName", "inputStream" }) })
	public String inputStream() {

		CustomInstructionImg cii = customInstructionImgService
				.get(getParameter("id"));
		byte[] imgFile = cii.getImgFile();
		if (null != cii && null != imgFile) {
			try {
				inputStream = new ByteArrayInputStream(imgFile);
			} finally {
				IOUtils.closeQuietly(inputStream);
			}
		}
		return "stream";
	}
	private ByteArrayInputStream inputStream;省略get/set
分享到:
评论

相关推荐

    getOutputStream() has already been called for this response 错误解决

    在Java Web开发中,"getOutputStream() has already been called for this response" 是一个常见的错误,通常出现在使用Servlet或JSP时。这个错误意味着在HTTP响应中,`getOutputStream()`已经被调用,然后尝试再次...

    tomcat6下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法

    1.在tomcat6.0下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法  在tomcat6.0下jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有...

    验证码出现getOutputStream()问题解决

    验证码出现getOutputStream() has already been called for this response错误解决

    java.lang.IllegalStateException: getOutputStream() has already解决办法

    纠结了半天的 java.lang.IllegalStateException: getOutputStream() has already。这个问题困扰了半天,在网上查阅了大量资料 出这个错误一般就是下面2个.....

    JSP彩色验证码

    解决了getOutputStream() has already been called for this response. 并将产生验证码的逻辑从JSP页面中分离出来,单独写了一个类 便于重用。

    Cannot forward after response has been committed

    "Cannot forward after response has been committed" 是一个在Java Web开发中常见的错误,通常与Servlet、Filter或控制器逻辑有关。这个错误表明服务器已经完成了对HTTP响应的处理,并将其发送到客户端,然后试图...

    handleStream ServletOutputStream out = response.getOutputStream(); InputStream i

    ServletOutputStream out = response.getOutputStream(); out.write("<html><body>Hello, World!</body></html>".getBytes()); out.flush(); out.close(); ``` 描述中的"InputStream i" 提到了输入流`InputStream`...

    response设置的实例源码

    本篇将深入探讨`response`设置的实例源码,以及如何在Servlet中应用这些设置。 一、Response对象的基本介绍 `HttpServletResponse`接口是`ServletResponse`接口的子接口,它扩展了通用的响应功能,以适应HTTP协议的...

    java.lang.IllegalStateException: OutputStream already obtain

    标题 "java.lang.IllegalStateException: OutputStream already obtain" 涉及到的是Java编程中的一个常见错误,特别是当处理I/O流时。这个异常通常在尝试获取已经存在的OutputStream实例时抛出,表明该输出流已经被...

    Response-Headers详解

    try (OutputStream out = response.getOutputStream()) { // 使用Apache POI或其他库生成Excel内容并写入out流 } ``` #### 五、动态生成JPEG图像 如果需要动态生成并发送JPEG格式的图像,也需要设置正确的响应...

    servlet2.4doc

    Returns a boolean indicating whether the named response header has already been set. contextDestroyed(ServletContextEvent) - Method in interface javax.servlet.ServletContextListener Notification ...

    java response 下载文件方法

    OutputStream ou = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); // 设置响应类型为二进制流 // 写入响应流并关闭 ou.write(buffer); ou....

    HttpServletRequest-response方法总结

    2. getOutputStream():从Servlet中可以通过getOutputStream方法取得ServletOutputStream对象,既可以输出字符数据,也可以输出MIME格式的二进制数据。 3. setContentType():在响应中可以表明内容格式和长度。 4. ...

    response生成图片验证

    这里使用了Servlet的`HttpServletResponse`对象,调用其`setContentType`方法设置响应的MIME类型为`image/jpeg`或`image/png`,然后使用`getOutputStream`获取输出流,并调用`ImageIO.write`方法将图片写入。...

    response jsp 中的重要知识点

    在Java Web开发中,`response.jsp`通常是指服务器端的响应页面,主要涉及Servlet和JSP(JavaServer Pages)技术。`response`对象是Servlet API中的一个关键组件,全称为`HttpServletResponse`,它用于构建并发送回...

    JSP内置对象request和response.doc

    JSP内置对象request和response详解 JSP内置对象request和response是JSP开发中两个非常重要的对象,它们分别用于处理客户端请求和响应信息。在本文档中,我们将详细介绍request和response对象的功能和使用方法。 ...

    response中文乱码解决的代码

    3. 使用`FileInputStream`读取文件内容,并通过`response.getOutputStream().write()`将内容写入响应流。 通过以上方法,我们可以在处理中文文件名的文件下载时,有效避免乱码问题,确保用户能够正确下载并识别文件...

    JAVAEE中Servlet实例Response与Request对象方法调用范例

    3. `getOutputStream()`:返回ServletOutputStream,用于写入二进制数据到响应体,如文件下载。 4. `getWriter()`:返回PrintWriter,用于写入文本数据到响应体,如HTML、JSON等。 在实际应用中,我们通常会创建一...

Global site tag (gtag.js) - Google Analytics