(Spring boot+;Java)为大于1 GB的文件下载API
我正在创建RESTAPI,用于在SpringBoot和java中下载文件。 我用这个例子 这里给出了三个示例,我尝试了前两个,我的文件大小大于1 GB: 1) ByteArrayResource: 这会导致我的服务器内存不足 2) InputStreamResource:这将生成java.io.EOFException 在这两种情况下,每当我的文件在300到400 MB之后增加时,下载就会停止,服务器就会失败 请建议如何制作一个更好的下载API,它对于较大的文件不会失败 编辑:我尝试了评论中给出的所有建议,但在所有方面我都只得到了这些建议,还添加了日志(Spring boot+;Java)为大于1 GB的文件下载API,java,spring-boot,Java,Spring Boot,我正在创建RESTAPI,用于在SpringBoot和java中下载文件。 我用这个例子 这里给出了三个示例,我尝试了前两个,我的文件大小大于1 GB: 1) ByteArrayResource: 这会导致我的服务器内存不足 2) InputStreamResource:这将生成java.io.EOFException 在这两种情况下,每当我的文件在300到400 MB之后增加时,下载就会停止,服务器就会失败 请建议如何制作一个更好的下载API,它对于较大的文件不会失败 编辑:我尝试了评论中给出
ERROR o.s.c.s.i.web.ExceptionLoggingFilter - Uncaught exception thrown
org.eclipse.jetty.io.EofException: null
at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:286)
at org.eclipse.jetty.io.WriteFlusher.flush(WriteFlusher.java:429)
at org.eclipse.jetty.io.WriteFlusher.completeWrite(WriteFlusher.java:384)
at org.eclipse.jetty.io.ChannelEndPoint$3.run(ChannelEndPoint.java:133)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:295)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126)
at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:762)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:680)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:51)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:264)
... 11 common frames omitted
您可以尝试:
@GetMapping(
value = "/download",
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<?> download() throws Exception {
File file = new File("/my/file/path");
org.springframework.core.io.UrlResource resource = new org.springframework.core.io.UrlResource(file.toURI());
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.contentLength(resource.contentLength())
.header(
HttpHeaders.CONTENT_DISPOSITION,
String.format("attachment; filename=\"%s\"", resource.getFilename()))
.body(resource);
}
@GetMapping(
value=“/download”,
products=MediaType.APPLICATION\u OCTET\u STREAM\u VALUE)
public ResponseEntity download()引发异常{
File File=新文件(“/my/File/path”);
org.springframework.core.io.UrlResource=new org.springframework.core.io.UrlResource(file.toURI());
返回ResponseEntity.ok()
.contentType(MediaType.APPLICATION\u八位字节\u流)
.contentLength(资源.contentLength())
.标题(
HttpHeaders.CONTENT\u处置,
String.format(“附件;文件名=\%s\”,resource.getFilename())
.机构(资源);
}
您可以尝试:
@GetMapping(
value = "/download",
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<?> download() throws Exception {
File file = new File("/my/file/path");
org.springframework.core.io.UrlResource resource = new org.springframework.core.io.UrlResource(file.toURI());
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.contentLength(resource.contentLength())
.header(
HttpHeaders.CONTENT_DISPOSITION,
String.format("attachment; filename=\"%s\"", resource.getFilename()))
.body(resource);
}
@GetMapping(
value=“/download”,
products=MediaType.APPLICATION\u OCTET\u STREAM\u VALUE)
public ResponseEntity download()引发异常{
File File=新文件(“/my/File/path”);
org.springframework.core.io.UrlResource=new org.springframework.core.io.UrlResource(file.toURI());
返回ResponseEntity.ok()
.contentType(MediaType.APPLICATION\u八位字节\u流)
.contentLength(资源.contentLength())
.标题(
HttpHeaders.CONTENT\u处置,
String.format(“附件;文件名=\%s\”,resource.getFilename())
.机构(资源);
}
尝试使用BufferedReaderGoogle for spring rest模板下载大文件将其包装。请检查您是否可以尝试直接使用Java NIOWrite将其写入输出流,不要使用其他2个选项。尝试使用BufferedReaderGoogle for spring rest模板下载大文件将其包装。请检查您是否可以尝试使用Java NIOWrite将其写入输出流直接输出流,不使用其他2个选项。