Will Spring hold contents in memory or stores in the disk?

前端 未结 4 1113
情话喂你
情话喂你 2021-02-07 20:43

When a file say 100 MB size is uploaded from browser will Spring hold whole data in memory or stores in the disk temporarily. After going through Spring doc I know how to set a

相关标签:
4条回答
  • 2021-02-07 21:04

    If you don't set the temp directory CommonsMultipartResolver will save temporary files to the servlet container's temporary directory.

    The "file" object in your example doesn't hold the data it similar to a java.io.File reference. You need to get the data with file.getBytes().

    0 讨论(0)
  • 2021-02-07 21:05

    Yes, but if it's stored on disk it will be deleted after the request has been processed. You can set the threshold for when it will be stored on disk:

    In your multipartresolver bean definition, e.g.:

    <property name="maxUploadSize" value="1000000" /> 
    <property name="maxInMemorySize" value="1000" /> 
    

    If it's held in memory, you could store it in the session and process it in the next request, for instance, such as if you're waiting for a user confirmation.

    0 讨论(0)
  • 2021-02-07 21:22

    A bit more digging in the javadoc shows that the default maximum in-memory size is 10240 bytes. From that I'd assume that any upload less than 10kB is held in memory, anything larger will be stored on disk. If you don't specify the disk location, it'll likely use a default (I'd guess it'll use the system default tmp directory).

    0 讨论(0)
  • 2021-02-07 21:22

    In spring boot, spring.servlet.multipart.file-size-threshold specifies the size threshold after which files will be written to disk.

    The default value of the property is zero. It means by default, it stores all files in a temporary location (can be configured via spring.servlet.multipart.location).

    Link: spring boot properties

    Be aware of some bugs on the implementations here: https://github.com/spring-projects/spring-boot/issues/9073

    0 讨论(0)
提交回复
热议问题