前言
下载文件报错
java.lang.IllegalStateException: Cannot call reset() after response has been committed
at org.apache.catalina.connector.ResponseFacade.reset(ResponseFacade.java:347)
at ins.platfrom.services.FileService.downloadTemplate2(FileService.java:1215)
at ins.platfrom.controller.FileController.downloadTemplate(FileController.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
原因
响应需要提前清空 然后再获取输出流 顺序不能颠倒 测试中发现
// 清空response
response.reset();
fis =new FileInputStream(fileName);
out = response.getOutputStream();