gpt4 book ai didi

java - wicket 8 : How to prevent sendRedirect after writing to response stream?

转载 作者:行者123 更新时间:2023-12-01 18:49:59 26 4
gpt4 key购买 nike

我有一个表单,可以在 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()。如何阻止 Wicket 在 onSubmit 之后调用 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]

最佳答案

你尝试过这个吗?

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);

关于java - wicket 8 : How to prevent sendRedirect after writing to response stream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59756589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com