gpt4 book ai didi

java - (Spring boot + Java) 大于 1 GB 文件的下载 API

转载 作者:行者123 更新时间:2023-12-01 23:11:32 25 4
gpt4 key购买 nike

我正在创建用于在 Spring Boot 和 Java 中下载文件的 REST API。我用过这个https://o7planning.org/en/11765/spring-boot-file-download-example例如。

这里给出了三个例子,我尝试了前两个,我的文件大小大于1 GB:

1) 字节数组资源:这会导致我的服务器内存不足

2) InputStreamResource:这会产生 java.io.EOFException

在这两种情况下,每当我的文件增加到 300 到 400 MB 之后,下载就会停止并且服务器出现故障。

请建议如何制作更好的下载 API,该 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);

}

关于java - (Spring boot + Java) 大于 1 GB 文件的下载 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58373954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com