Reputation: 101
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
Upvotes: 2
Views: 2748
Reputation: 27018
Two issues here
The error you see is because of your jvm setting. Check your memory setting.(-Xms and -Xmx)
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.
Upvotes: 3
Reputation: 4476
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
Upvotes: 2