java Spring MVC:下载大文件,OutOfMemoryException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15800565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:53:55  来源:igfitidea点击:

Spring MVC : large files for download, OutOfMemoryException

javaspring-mvcdownload

提问by Martin V.

How to provide large files for download through spring controller ? I followed few discussions on similar topic :

如何通过 spring 控制器提供大文件下载?我关注了一些关于类似主题的讨论:

Downloading a file from spring controllers

从弹簧控制器下载文件

but those solutions fails for large files ~ 300mb - 600mb. I am getting OutOfMemoryException on the last line :

但是这些解决方案对于大约 300mb - 600mb 的大文件失败了。我在最后一行收到 OutOfMemoryException :

@RequestMapping(value = "/file/{dummyparam}.pdf", method = RequestMethod.GET, produces=MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody byte[] getFile(@PathVariable("dummyparam") String dummyparam, HttpServletResponse response) {
.
.       
InputStream is = new FileInputStream(resultFile);

response.setHeader("Content-Disposition", "attachment; filename=\"dummyname " + dummyparam + ".pdf\"");
.
.         
return IOUtils.toByteArray(is);

My (naive) assumption was that IOUtils will handle even large files but this is not obviously happening. Is there any way how to split file into chunks as download is in progress ? Files are usually around 300 - 600mb large. Max number of concurrent downloads is estimated to 10.

我(天真)的假设是 IOUtils 甚至可以处理大文件,但这显然不会发生。有没有什么办法可以在下载过程中将文件拆分成块?文件通常大约 300 - 600mb 大。最大并发下载数估计为 10。

Easy way would be to link files as static content in the webserver directory but we would like to try do it in within our Spring app.

简单的方法是将文件链接为 webserver 目录中的静态内容,但我们想尝试在我们的 Spring 应用程序中执行此操作。

回答by Arun P Johny

It is because you are reading the entire file into memory, use a buffered read and write instead.

这是因为您正在将整个文件读入内存,请改用缓冲读写。

@RequestMapping(value = "/file/{dummyparam}.pdf", method = RequestMethod.GET, produces=MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void getFile(@PathVariable("dummyparam") String dummyparam, HttpServletResponse response) {


    InputStream is = new FileInputStream(resultFile);

    response.setHeader("Content-Disposition", "attachment; filename=\"dummyname " + dummyparam + ".pdf\"");


    int read=0;
    byte[] bytes = new byte[BYTES_DOWNLOAD];
    OutputStream os = response.getOutputStream();

    while((read = is.read(bytes))!= -1){
        os.write(bytes, 0, read);
    }
    os.flush();
    os.close(); 
}

回答by kamlesh0606

For Spring , Need use InputStreamResourceclass in ResponseEntity .

对于 Spring,需要在 ResponseEntity 中使用InputStreamResource类。

Demo Code :

演示代码:

        MediaType mediaType = MediaTypeUtils.getMediaTypeForFileName(this.servletContext, fileName);
        System.out.println("fileName: " + fileName);
        System.out.println("mediaType: " + mediaType);

        File file = new File(DIRECTORY + "/" + fileName);
        InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

        return ResponseEntity.ok()
                // Content-Disposition
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName())
                // Content-Type
                .contentType(mediaType)
                // Contet-Length
                .contentLength(file.length()) //
                .body(resource);
    }

Ref Link : https://o7planning.org/en/11765/spring-boot-file-download-example

参考链接:https: //o7planning.org/en/11765/spring-boot-file-download-example