我正在尝试使用以下代码段在浏览器中打开pdf文件:
public ActionResult action(Request request, AspectModel arg1)
throws ServletException {
if ("PDF".equals(request.event())) {
try{
HttpServletResponse resp=(HttpServletResponse) request.getResponse();
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition","inline;filename=AccountOpeningSummary.pdf");
resp.setHeader("Cache-Control", "cache, must-revalidate");
resp.setHeader("Cache-Control", "no-store,public");
resp.setDateHeader("Expires", 0);
resp.setHeader("Pragma", "public");
OutputStream OutStream = resp.getOutputStream();
OutStream.write(infos,0,infos.length);
OutStream.flush();
OutStream.close();
}catch (Exception e) {
System.out.println("PDF Write Exception...........................####"+e);
}
}
return null;
}
我收到以下错误:
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:215)
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.JspWriterImpl.flush(JspWriterImpl.java:177)
at abaxx.web.taglib.IncludeTag.doEndTag(IncludeTag.java:69)
at org.apache.jsp.parts.index_jsp._jspx_meth_xx_005finclude_005f0(index_jsp.java:237)
at org.apache.jsp.parts.index_jsp._jspService(index_jsp.java:93)
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:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
at abaxx.core.foundation.internal.SelfDestroyingHttpServletRequestWrapper$1.include(SelfDestroyingHttpServletRequestWrapper.java:36)
at abaxx.core.foundation.internal.SelfDestroyingHttpServletRequestWrapper$1.include(SelfDestroyingHttpServletRequestWrapper.java:36)
at abaxx.web.parts.internal.ServletRequestContext.doInclude(ServletRequestContext.java:85)
at abaxx.web.parts.RequestContext.include(RequestContext.java:429)
at abaxx.web.parts.VisualPart.basicDisplay(VisualPart.java:740)
at abaxx.web.parts.CompositePart.basicDisplay(CompositePart.java:111)
at abaxx.web.parts.VisualPart.doDisplay(VisualPart.java:762)
at abaxx.web.control.DisplayLoopInterceptor.display(DisplayLoopInterceptor.java:233)
at abaxx.web.security.StandardSecurityPolicy$DisplayHook.display(StandardSecurityPolicy.java:321)
at abaxx.web.control.PerformanceMonitor$DisplayPerformanceInterceptor.display(PerformanceMonitor.java:187)
at abaxx.web.control.DisplayLoopInterceptor.doDisplay(DisplayLoopInterceptor.java:80)
at abaxx.web.parts.VisualPart.display(VisualPart.java:104)
at abaxx.web.control.DisplayLoopInterceptor.process(DisplayLoopInterceptor.java:128)
at vanilla.portal.builder.PBDisplayLoopInterceptor.process(PBDisplayLoopInterceptor.java:60)
at vanilla.common.EncodingExtension$ResponseEncodingInterceptor.process(EncodingExtension.java:107)
at abaxx.web.preferences.PreferencesHookup$2.process(PreferencesHookup.java:126)
at vanilla.common.HttpHeaderInterceptor.process(HttpHeaderInterceptor.java:36)
at abaxx.web.control.PageflowInterceptor.process(PageflowInterceptor.java:62)
at abaxx.workflow.pageflow.PageflowInterceptor.process(PageflowInterceptor.java:93)
at abaxx.web.control.ActionInterceptor.process(ActionInterceptor.java:49)
at abaxx.web.security.StandardSecurityPolicy$ActionHook.process(StandardSecurityPolicy.java:211)
at abaxx.web.control.SessionTimeoutInterceptor.process(SessionTimeoutInterceptor.java:59)
at abaxx.track.TrackingInterceptor.finishTracking(TrackingInterceptor.java:151)
at abaxx.track.TrackingInterceptor.process(TrackingInterceptor.java:100)
at abaxx.web.control.AccessViolationInterceptor.process(AccessViolationInterceptor.java:110)
at abaxx.web.control.RequestProcessor$P9.process(RequestProcessor.java:72)
at abaxx.web.control.PerformanceMonitor$RequestPerformanceInterceptor.process(PerformanceMonitor.java:153)
at vanilla.common.EncodingExtension$RequestEncodingInterceptor.process(EncodingExtension.java:61)
at abaxx.web.control.MaintenanceInterceptor.process(MaintenanceInterceptor.java:44)
at abaxx.web.control.AbaXXComponentsHookup$ClientSessionHookupInterceptor.process(AbaXXComponentsHookup.java:42)
at abaxx.web.security.SecurityPolicy$FakeContainerLoginInterceptor.process(SecurityPolicy.java:136)
at abaxx.web.control.RequestProcessor.process(RequestProcessor.java:187)
at abaxx.web.control.ApplicationServlet.doGet(ApplicationServlet.java:144)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at abaxx.web.control.url.RequestFilter.doFilter(RequestFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at abaxx.web.control.CachingHeaderFilter.doFilter(CachingHeaderFilter.java:29)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at vanilla.common.EncodingExtension.doFilter(EncodingExtension.java:121)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
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:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
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:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:301)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:162)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
我还尝试使用以下方式关闭getWriter()
:
HttpServletResponse resp=(HttpServletResponse) request.getResponse();
Writer rt=resp.getWriter();
resp.getWriter().close();
有人能帮我吗?
您使用的是哪个版本的abaxx?在最近的版本中,您可以返回ActionResult。如果您在控制器中生成响应,则从action方法中中止。如果您返回null,框架将继续显示阶段并呈现JSP。
这是因为您已通过将使用响应输出流的PDF生成代码放在JSP文件中
要解决您的设计问题,您需要创建一个扩展
HttpServlet
的简单类,将所有Java代码移动到doGet()
方法(或doPost()
,具体取决于您想响应的请求类型)。最后通过@WebServlet
注释或将该servlet映射到所需的URL模式上
下面是一个基本的启动示例,假设您使用的是与Servlet 3.0兼容的容器,如Tomcat 7、Glassfish 3等,以便您可以使用WebServlet注释:
@WebServlet("/pdfServlet")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Put your code here.
}
}
。通过调用它http://localhost:8080/contextname/pdfServlet
而不是索引。jsp
如果您的要求是在HTML页面中内联显示PDF,那么您需要<代码>
<iframe src="pdfServlet"></iframe>
这样,浏览器将通过完全独立的HTTP请求下载PDF文件。
编辑:现在问题已经发生了重大变化。。。
听起来JSP仍在尝试将自己呈现为响应。您需要告诉容器您正在处理请求——理想情况下,您不应该接近JSP,因为这可能只是针对PDF的请求。您应该将容器配置为调用单独的servlet来下载PDF;这样,您就根本不会涉及JSP部分。
(是的,这将意味着移动问题中显示的代码。这可能是当前JSP的一部分;不应该是。)
我有一个包含表的JSP页面。加载页面时,将填充表。我还有一个每隔X秒的ajax调用,它必须刷新表内容。 加载时,按预期填充内容。但在ajax调用期间,它会失败,并出现以下错误: 我检查了这个问题上存在的问题,但没有什么好结果。我没有在代码中使用scriplets。 JSP代码: 控制器代码:
最大的问题是,对于以下异常,代码片段可以正常工作。用户可以将文档保存在所需的位置。我想弄清楚为什么我会得到这个错误。
我正在用Spring做一个项目,我有这个问题,我谷歌了错误信息,我找到了解决方案,甚至所有关于这个问题的帖子 有人能帮忙吗?
问题内容: 我谷歌的错误消息 ,很多人说,这是因为后面的空格或换行,但在我的代码,没有一个空格或一个换行符。我在Linux上使用tomcat6。 根本原因 问题答案: 好的,你应该使用servlet而不是JSP,但是如果你确实需要…在页面顶部添加以下指令: 或者在jsp-config部分中,你的web.xml 此外flush/ close中OutputStream和返回完成时。
我正在做关于CXF和Spring的培训,所以我写了一个非常简单的CXF演示,其中只有接口“HelloWorld”和它的实现者“HelloWorldWs”。 我想用Tomcat发布它。我编写了web.xml和applicationcontext.xml(Spring配置文件。虽然我可以发布WSDL。但是控制台列出了一个问题: 我没有使用或编写任何io函数,只是一个“sayhi”函数。我被难倒了。