简体   繁体   中英

spring mvc download file IOException

i use spring mvc download big file;

Why does this happen, When I use nginx to simulate file download, there is no problem. I try HTTP response

I tried about 30 MB of video

CODE:

@GetMapping({"{id}"})
public void get(@PathVariable String id, HttpServletResponse response) throws IOException {
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    response.setHeader("Content-Disposition", ContentDisposition.parse("filename=12.mp4").toString());
    response.setHeader("Connection", "keep-alive");
    response.setStatus(HttpStatus.OK.value());

    InputStream inputStream = .....;
    OutputStream outputStream = response.getOutputStream()
    StreamUtils.copy(inputStream, outputStream);
    inputStream.close();
    outputStream.close();
}

ERROR:

org.apache.catalina.connector.ClientAbortException: java.io.IOException: An established connection was aborted by the software in your host machine
    at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:341)
    at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:766)
    at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:671)
    at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:376)
    at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:354)
    at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96)
    at org.springframework.util.StreamUtils.copy(StreamUtils.java:143)
    at com.wzx.oss.service.FileService.writeFileToOutputStream(FileService.java:331)
    at com.wzx.oss.controller.FileController.handleFileTransfer(FileController.java:79)
    at com.wzx.oss.controller.FileController.get(FileController.java:44)
    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:189)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
...

My problem has been solved

Thank you very much for your answer, @Octavian R.

Content-Disposition: The protocol determines how the program is to be saved

Response headers need to be added, not caching

response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "No-cache");
response.setDateHeader("Expires", 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM