将 HttpServlet 与 JAVA 一起使用时出现关闭错误

Closed error when using HttpServlet with JAVA

我写了 HttpServlet doGet 方法这是 servlet:

public class PrintDocsServlet extends HttpServlet {

    private static final int BAD_REQUEST = 400;


    public void init() throws ServletException {

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String id = request.getParameter("id");
            String format = request.getParameter("format");
            user_format = format == null ? "pdf" : format;
            
            if(id != null && !id.isEmpty())
            {
                printFile(response, id, user_format);
            }
            else
            {
                response.sendError(BAD_REQUEST);
            }

        } catch(Exception e) {
            session = null;
            e.printStackTrace();
            response.sendError(BAD_REQUEST);
        } finally {
            if(session != null)
            {
                DfcSessions.release(session);
            }
        }
    }

    private void printFile(HttpServletResponse response, String id, String format) throws DfException, IOException {


            ByteArrayInputStream content = null;

            String fileName = "view." + format;
            
            response.setContentType(fileType);
            response.addHeader("Content-Length", Long.toString(obj.getContentSize()));
            

            FileInputStream content = new FileInputStream("C:/demo.pdf");

            ServletOutputStream out = response.getOutputStream();

            IOUtils.copy(content, out);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);

            if(content != null)
            {
                content.close();
            }
            if(out != null)
            {
                out.flush();
                out.close();
            }
        }
    }
}

此代码在端口 8888 上与 jetty 8.1.16 完美配合,但当我使用 jetty 9.4.38 时出现此错误:

org.eclipse.jetty.io.EofException: Closed
       at org.eclipse.jetty.server.HttpOutput.checkWritable(HttpOutput.java:768)
       at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:792)
       at com.github.ziplet.filter.compression.ThresholdOutputStream.write(ThresholdOutputStream.java:93)
       at com.github.ziplet.filter.compression.CompressingServletOutputStream.write(CompressingServletOutputStream.java:67)
       at org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:645)
       at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2147)
       at org.apache.commons.io.IOUtils.copy(IOUtils.java:2102)
       at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2123)
       at org.apache.commons.io.IOUtils.copy(IOUtils.java:2078)
       at com.servlets.PrintDocsServlet.printFile(PrintDocsServlet.java:128)
       at com.servlets.PrintDocsServlet.doGet(PrintDocsServlet.java:57)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
       at org.eclipse.jetty.servlet.ServletHolder$NotAsync.service(ServletHolder.java:1452)
       at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:791)
       at org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1626)
       at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:155)
       at com.emc.xcp.rest.security.filter.BasicAuthFilter.doFilterInternal(BasicAuthFilter.java:62)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter.doFilterInternal(DefaultLogoutPageGeneratingFilter.java:52)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:216)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at com.emc.xcp.rest.security.filter.CSRFTokenFilter.doFilter(CSRFTokenFilter.java:22)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
       at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at com.emc.xcp.rest.security.filter.RepositoryInfoFilter.doFilter(RepositoryInfoFilter.java:33)
       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at com.emc.xcp.ui.LocaleFilter.doFilter(LocaleFilter.java:26)
       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
       at com.emc.xcp.ui.web.filter.CustomCharEncodingFilter.doFilterInternal(CustomCharEncodingFilter.java:43)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at com.emc.xcp.ui.RestCorsFilter.doFilterInternal(RestCorsFilter.java:49)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at com.github.ziplet.filter.compression.CompressingFilter.doFilter(CompressingFilter.java:304)
       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
       at com.emc.xcp.ui.CompressingFilterProxy.doFilter(CompressingFilterProxy.java:38)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:201)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:548)
       at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
       at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:602)
       at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
       at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)
       at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624)
       at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)
       at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1435)
       at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)
       at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501)
       at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594)
       at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)
       at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1350)
       at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
       at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:234)
       at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
       at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
       at org.eclipse.jetty.server.Server.handle(Server.java:516)
       at org.eclipse.jetty.server.HttpChannel.lambda$handle(HttpChannel.java:388)
       at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:633)
       at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380)
       at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:279)
       at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
       at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)
       at org.eclipse.jetty.io.ChannelEndPoint.run(ChannelEndPoint.java:104)
       at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336)
       at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313)
       at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171)
       at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129)
       at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:383)
       at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:882)
       at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1036)
       at java.base/java.lang.Thread.run(Thread.java:834)
2022-03-29 08:17:49.614:WARN:oejs.HttpChannel:qtp1971764991-171: /MevakerApp/PrintDocsServlet
java.lang.IllegalStateException: COMPLETED
       at org.eclipse.jetty.server.HttpChannelState.sendError(HttpChannelState.java:896)
       at org.eclipse.jetty.server.Response.sendError(Response.java:471)
       at org.eclipse.jetty.server.Response.sendError(Response.java:442)
       at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:132)
       at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:132)
       at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:132)
       at org.springframework.security.web.util.OnCommittedResponseWrapper.sendError(OnCommittedResponseWrapper.java:115)
       at com.servlets.PrintDocsServlet.doGet(PrintDocsServlet.java:67)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
       at org.eclipse.jetty.servlet.ServletHolder$NotAsync.service(ServletHolder.java:1452)
       at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:791)
       at org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1626)
       at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:155)
       at com.emc.xcp.rest.security.filter.BasicAuthFilter.doFilterInternal(BasicAuthFilter.java:62)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter.doFilterInternal(DefaultLogoutPageGeneratingFilter.java:52)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:216)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at com.emc.xcp.rest.security.filter.CSRFTokenFilter.doFilter(CSRFTokenFilter.java:22)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
       at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at com.emc.xcp.rest.security.filter.RepositoryInfoFilter.doFilter(RepositoryInfoFilter.java:33)
       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
       at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
       at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
       at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)

在这一行:

IOUtils.copy(content, out);

我能做什么?考虑到我无法更改码头版本,我该怎么办? 我尝试使用 IOUtils.copy 替代方法,但出现相同的错误,错误提示 servletresponseoutput 已关闭。

tnx

一些事情...

  • 在 Servlet 3.1+ 中无法再通过 Servlet 过滤器完成压缩(您可以感谢 Async I/O 对规范所做的必要更改)。
    请改用 Jetty built-in GzipHandler

  • 压缩时不要设置Content-Length响应header(你必须pre-compress才能知道实际的内容长度)

  • Content-Type 响应 header 设置为适合您的内容的值。

  • 在您写回复之前设置所有回复 headers。在响应 body 内容写入后,您无法更改 headers(HttpServletResponse.isCommitted() 告诉您是否可以再更改响应)