gpt4 book ai didi

java - 无法在 Spring boot 2 中上传 1gb 文件

转载 作者:行者123 更新时间:2023-12-01 19:35:03 26 4
gpt4 key购买 nike

我正在尝试上传1GB的视频文件。但服务器不允许上传。

它返回以下错误:

15:46:02.164 [http-nio-8085-exec-10] ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/broadcast].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/broadcast] threw exception [Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: Java heap space] with root cause
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3745) ~[?:?]
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:120) ~[?:?]
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:95) ~[?:?]
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:156) ~[?:?]
at org.springframework.util.StreamUtils.copy(StreamUtils.java:143) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:110) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.util.FileCopyUtils.copyToByteArray(FileCopyUtils.java:162) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.getBytes(StandardMultipartHttpServletRequest.java:245) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]

执行以下代码时出现此错误:

File file = new File(multipartFile.getOriginalFilename());
FileOutputStream fos = new FileOutputStream(file);
fos.write(multipartFile.getBytes());
fos.close();

在java文件中。

下面是我的“SpringToolSuite4.ini”文件

-startup
plugins/org.eclipse.equinox.launcher_1.5.400.v20190515-0925.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.1000.v20190125-2016
-product
org.springframework.boot.ide.branding.sts4
--launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion=1.8
-Xms256m
-Xmx1024m
-XX:+UseG1GC
-XX:+UseStringDeduplication
--add-modules=ALL-SYSTEM

最佳答案

这是由于 JVM 分配造成的,当您尝试上传 1G 文件时,实际上您只为您的 JVM 分配了 1G,并且 getBytes() 会将所有数据复制到您的内存中,但是确实没有足够的内存。

所以方法是1)你可以尝试为你的JVM分配更多的内存,但实际上不建议这样做。 2)这是一个很好的问题/答案,也许更适合您的情况选择:SpringBoot: Large Streaming File Upload Using Apache Commons FileUpload

关于java - 无法在 Spring boot 2 中上传 1gb 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58165948/

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