0

我有一个表单,它在 onSubmit() 结束时将 PDF 文件下载给用户。

   @Override
   protected void onSubmit() {
       if (some error condition) {
          getPage().error("Form invalid for some reason");
          return;
       }
       byte[] bytes = ...; 
       WebResponse response = getWebResponse();
       response.setContentType("application/pdf");
       response.setStatus(200);
       response.setAttachmentHeader("file.pdf"));
       response.write(bytes));
   }   

当我运行代码时,onSubmit 正确地允许用户下载 PDF 文件。然而,日志文件得到 19 条堆栈跟踪抱怨:在提交响应后无法调用 sendRedirect()。如何在我的 onSubmit 之后阻止 Wicket 调用 sendRedirect?

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
    at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:488) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.wicket.protocol.http.servlet.ServletWebResponse.sendRedirect(ServletWebResponse.java:288) ~[wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.protocol.http.HeaderBufferingWebResponse.sendRedirect(HeaderBufferingWebResponse.java:117) ~[wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.request.handler.render.WebPageRenderer.redirectTo(WebPageRenderer.java:161) ~[wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.request.handler.render.WebPageRenderer.respond(WebPageRenderer.java:280) ~[wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.core.request.handler.RenderPageRequestHandler.respond(RenderPageRequestHandler.java:202) ~[wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:914) ~[wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.request.RequestHandlerExecutor.execute(RequestHandlerExecutor.java:65) ~[wicket-request-8.6.1.jar:8.6.1]
    at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:282) [wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:253) [wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:221) [wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.protocol.http.WicketFilter.processRequestCycle(WicketFilter.java:275) [wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:206) [wicket-core-8.6.1.jar:8.6.1]
    at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:299) [wicket-core-8.6.1.jar:8.6.1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_201]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_201]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_201]
4

1 回答 1

0

你试过这个吗?

final AbstractResourceStreamWriter rstream = new AbstractResourceStreamWriter()
{
     @Override  
     public String getContentType() 
     {
          return "application/pdf";
     }

     @Override
     public void write(OutputStream out)
     {
         try  {
             out.write(bytes,0,bytes.length);
         }                      
         catch(IOException e) { /* ... */ }
     }
};
final ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(rstream, "my.pdf");
getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
于 2020-02-17T11:37:21.087 回答