简体   繁体   English

无法在 Spring 启动 2 中上传 1gb 文件

[英]Not able to upload 1gb file in Spring boot 2

I am trying to upload the video file of 1gb.我正在尝试上传 1gb 的视频文件。 But Server is not allowing to upload it.但是服务器不允许上传它。

It is returning below error:它返回以下错误:

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]

I am getting this error while executing below code:执行以下代码时出现此错误:

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

in java file.在 java 文件中。

Below is my "SpringToolSuite4.ini" file下面是我的“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

Two issues here这里有两个问题

  1. The error you see is because of your jvm setting.您看到的错误是因为您的 jvm 设置。 Check your memory setting.(-Xms and -Xmx)检查您的 memory 设置。(-Xms 和 -Xmx)

  2. By default you cannot upload such big file in springboot.默认情况下,你不能在 springboot 中上传这么大的文件。 You need to increase the default limit (10 MB)您需要增加默认限制 (10 MB)

BeforeSpring Boot 2.0:在 Spring Boot 2.0 之前:

spring.http.multipart.max-file-size=-1
spring.http.multipart.max-request-size=-1

After Spring Boot 2.0: Spring 启动 2.0 后:

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1

-1 means no limit. -1 表示没有限制。 It is highly recommended not to use -1 and instead use a specific value.强烈建议不要使用 -1 而是使用特定值。

This is due to the JVM alloation, actullay you only alloac 1G for your JVM, when you try to upload 1G file, and the getBytes() will copy all the data to your memory, but there is no enough memory indeed. This is due to the JVM alloation, actullay you only alloac 1G for your JVM, when you try to upload 1G file, and the getBytes() will copy all the data to your memory, but there is no enough memory indeed.

So the approach is 1) you can try to alloce more memory for your JVM, but this is really not suggestted.所以方法是1)你可以尝试为你的JVM分配更多的memory,但这真的不建议。 2) Here is a good question/answer maybe a more better chhoose for your case: SpringBoot: Large Streaming File Upload Using Apache Commons FileUpload 2)这是一个很好的问题/答案,对于您的情况可能是一个更好的选择: SpringBoot:使用 Apache Commons FileUpload 上传大型流文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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