1

I am trying to upload the video file of 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.

Below is my "SpringToolSuite4.ini" file

-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
user3526665
  • 81
  • 1
  • 1
  • 6
  • This is a problem with your JVM (Java virtual machine) memory limits, try increasing it to 2x the size of your file and see if it works! – ZektorH Sep 30 '19 at 10:29
  • Don't use `getBytes` because that will copy everything into memory. Use an input stream instead. Use something like `StreamUtils.copy(multipartFIle.getInputStream(), fos);` – M. Deinum Sep 30 '19 at 10:32
  • updated size -Xms2560m -Xmx3024m in .ini file but getting same error. – user3526665 Sep 30 '19 at 10:36
  • Thanks M. Deinum ! your solution worked for me.. – user3526665 Sep 30 '19 at 13:17

2 Answers2

2

Two issues here

  1. The error you see is because of your jvm setting. Check your memory setting.(-Xms and -Xmx)

  2. By default you cannot upload such big file in springboot. You need to increase the default limit (10 MB)

BeforeSpring Boot 2.0:

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

After Spring Boot 2.0:

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

-1 means no limit. It is highly recommended not to use -1 and instead use a specific value.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
2

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. 2) Here is a good question/answer maybe a more better chhoose for your case: SpringBoot: Large Streaming File Upload Using Apache Commons FileUpload

Liping Huang
  • 4,378
  • 4
  • 29
  • 46