[alibaba/easyexcel]resin服务器环境下,Web导出报错 getWriter() can't be called after getOutputStream()

2024-04-26 514 views
1

触发场景描述 resin服务器环境下 触发Bug的代码

List<DemoData> result = data();
EasyExcel.write(response.getOutputStream(), DemoData.class).sheet("sheet1").doWrite(result);

提示的异常或者没有达到的效果 getWriter() can't be called after getOutputStream()

回答

6

请提供详细异常 和详细代码

6

代码应该影响不大,问题主要是针对某些容器类型的,本地jetty可以运行,线上使用的resin(已验证),可能welogic也会报错(未验证) 项目框架 SSM 版本依赖: `

com.alibaba easyexcel 2.0.5
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.17</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>

`

涉及功能,web导出 代码 @Get("/ot/export/pa") public void paymanageExport(@Param("a") String a, @Param("b") String b, HttpServletRequest request, HttpServletResponse response) throws Exception { List<DataRow> result = Lists.newArrayList(); ImmutablePair<Boolean, Object> ret = homeDataService.getTableData(a, b); String fileName = URLEncoder.encode(homeDataService.fileNameGenerator(a, b), "UTF-8"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); //URLEncoder.encode防止中文乱码 String fileName = URLEncoder.encode(homeDataService.fileNameGenerator(a, b), "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), DataRow.class).sheet("sheet1").doWrite(result); } 实体类 @Data public class DataRow implements Bean { @ExcelProperty("序号") private int rank; @ExcelProperty("编码") private String code; @ExcelProperty("名称") private String name; }

3

请提供详细异常

4

org.springframework.web.util.NestedServletException: GET /api/export; nested exception is java.lang.IllegalStateException: getWriter() can't be called after getOutputStream(). at net.paoding.rose.RoseFilter.throwServletException(RoseFilter.java:551) at net.paoding.rose.RoseFilter.doFilter(RoseFilter.java:349) at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:89) at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:164) at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:89) at com.filter.LogIdFilter.doFilter(LogIdFilter.java:34) at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:89) at com.filter.CorsFilter.doFilter(CorsFilter.java:56) at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:89) at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:156) at com.caucho.server.webapp.AccessLogFilterChain.doFilter(AccessLogFilterChain.java:95) at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:290) at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:838) at com.caucho.network.listen.TcpSocketLink.dispatchRequest(TcpSocketLink.java:1353) at com.caucho.network.listen.TcpSocketLink.handleRequest(TcpSocketLink.java:1309) at com.caucho.network.listen.TcpSocketLink.handleRequestsImpl(TcpSocketLink.java:1293) at com.caucho.network.listen.TcpSocketLink.handleRequests(TcpSocketLink.java:1201) at com.caucho.network.listen.TcpSocketLink.handleAcceptTaskImpl(TcpSocketLink.java:997) at com.caucho.network.listen.ConnectionTask.runThread(ConnectionTask.java:117) at com.caucho.network.listen.ConnectionTask.run(ConnectionTask.java:93) at com.caucho.network.listen.SocketLinkThreadLauncher.handleTasks(SocketLinkThreadLauncher.java:169) at com.caucho.network.listen.TcpSocketAcceptThread.run(TcpSocketAcceptThread.java:61) at com.caucho.env.thread2.ResinThread2.runTasks(ResinThread2.java:173) at com.caucho.env.thread2.ResinThread2.run(ResinThread2.java:118) Caused by: java.lang.IllegalStateException: getWriter() can't be called after getOutputStream(). at com.caucho.server.http.HttpServletResponseImpl.getWriter(HttpServletResponseImpl.java:183) at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:164) at org.springframework.session.web.http.OnCommittedResponseWrapper.getWriter(OnCommittedResponseWrapper.java:149) at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:164) at net.paoding.rose.web.instruction.TextInstruction.sendResponse(TextInstruction.java:132) at net.paoding.rose.web.instruction.TextInstruction.doRender(TextInstruction.java:118) at net.paoding.rose.web.instruction.AbstractInstruction.render(AbstractInstruction.java:44) at net.paoding.rose.web.instruction.InstructionExecutorImpl.render(InstructionExecutorImpl.java:46) at net.paoding.rose.web.impl.thread.RootEngine.execute(RootEngine.java:140) at net.paoding.rose.web.impl.thread.LinkedEngine.execute(LinkedEngine.java:69) at net.paoding.rose.web.impl.thread.Rose.doNext(Rose.java:121) at net.paoding.rose.web.impl.thread.Rose.innerStart(Rose.java:256) at net.paoding.rose.web.impl.thread.Rose.start(Rose.java:109) at net.paoding.rose.RoseFilter.doFilter(RoseFilter.java:346) at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:89) at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:164) at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:89) at com.filter.LogIdFilter.doFilter(LogIdFilter.java:34) at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:89) at com.filter.CorsFilter.doFilter(CorsFilter.java:56) at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:89) at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:156) at com.caucho.server.webapp.AccessLogFilterChain.doFilter(AccessLogFilterChain.java:95) at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:290) at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:838) at com.caucho.network.listen.TcpSocketLink.dispatchRequest(TcpSocketLink.java:1353) at com.caucho.network.listen.TcpSocketLink.handleRequest(TcpSocketLink.java:1309) at com.caucho.network.listen.TcpSocketLink.handleRequestsImpl(TcpSocketLink.java:1293) at com.caucho.network.listen.TcpSocketLink.handleRequests(TcpSocketLink.java:1201) at com.caucho.network.listen.TcpSocketLink.handleAcceptTaskImpl(TcpSocketLink.java:997) at com.caucho.network.listen.ConnectionTask.runThread(ConnectionTask.java:117) at com.caucho.network.listen.ConnectionTask.run(ConnectionTask.java:93) at com.caucho.network.listen.SocketLinkThreadLauncher.handleTasks(SocketLinkThreadLauncher.java:169) at com.caucho.network.listen.TcpSocketAcceptThread.run(TcpSocketAcceptThread.java:61) at com.caucho.env.thread2.ResinThread2.runTasks(ResinThread2.java:173) at com.caucho.env.thread2.ResinThread2.run(ResinThread2.java:118)

2

是不是 框架不一样 你尝试 设置参数 autoclosestream 为false 应该就行了