- How To Set Spring.http.multipart.location For Macbook Air
- How To Set Spring.http.multipart.location For Macbook Pro
- How To Set Spring.http.multipart.location For Macbook
Spring.velocity.expose-session-attributes=false # Set whether all HttpSession attributes should be added to the model prior to merging with the template. Spring.velocity.expose-spring-macro-helpers=true # Set whether to expose a RequestContext for use by Spring's macro library, under the name 'springMacroRequestContext'. Set file temporary storage path spring.http.multipart.location = /var/tmp. The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.
Last Update: 23.05.2017. By Jens in Spring Boot | Spring MVC
Uploading a file with Spring MVC is pretty easy, and it gets even easier when running in Spring Boot. However, it’s one of the tasks a developer does once in a while and therefore is pretty easy to forget. The page is a reminder for all of us.
Uploading File
For uploading a file, you must use multipart/form-data as the form encoding and usually files are uploaded with a POST request.
On the Spring MVC @Controller we only need to add a @RequestParam with the name of your form field and map it to a MultipartFile class.
How To Set Spring.http.multipart.location For Macbook Air
The MultipartFile class provides access to the file, file name, file type, etc.
Uploading Multiple Files
For uploading multiple files together, we only need to add all the files to the form.
In your form will have multiple instances of file inputs using the same name attribute. The browser will include them all in the request.
In the @Controller, we can simply switch from a single MultipartFile parameter to using a MultipartFile array.
Configuring Multipart File Uploads in Spring Boot
In Spring Boot the Multipart file upload is configured automatically and set up with defaults. It will also use the Servlet 3 API for Multiparts.
If you need to adjust the max file size or similar, you can do so by setting the following properties in your application.properties.
The most used properties are:
- spring.http.multipart.file-size-threshold A threshold after which the files are written to disk. Supports MB or KB as suffixes to indicate size in Megabyte or Kilobyte
- spring.http.multipart.location location of the temporary files
- spring.http.multipart.max-file-size Max size per file the upload supports; also supports the MB or KB suffixes; by default 1MB
- spring.http.multipart.max-request-size max size of the whole request; also supports the MB or KB suffixes
Conclusion
Uploading files with Spring MVC is straight forward, and with Spring Boot you get zero configuration too. And, in case you must change the file size or other, you can do so easily with the properties.
Want content like this in your inbox each workday irregularly? No BS, spam or tricks... just useful content:
一、Spring boot上传自动配置的有哪些?
1、自动配置MultipartAutoConfiguration类
配置文件在Spring-boot-autoconfig下org.springframework.boot.autoconfigure.web包内
MultipartAutoConfiguration类我们先看下他的关系图
springboot 的源代码如下:
从这开始我们来逐步分析一下。
我们那可以清楚的知道@ConditionalOnClass这类只是在特定的类加载后才使用。
使用@EnableConfigurationProperties开启使用自动配置的文件,当配置文件缺失的时候默认 spring.http.multipart.enabled为true
这就是说明默认的时候启用。但是看到这里我们好像还不太清楚我们使用的是什么上传方式,而且上传的具体处理我们好像还不是太清楚。那就继续往下看。
我们可以看下MultipartProperties这个类 ,其实从定义上我们清楚了这是一个资源文件类。
代码如下:这里面配置的都是默认情况的配置文件,包括临时目录,文件最大大小,和最大的请求大小等
重点来了:我们看下最后的一个创建 createMultipartConfig方法。这个方法很容易理解就是通过MultipartConfigFactory 来设置上面说的配置项。
其实我们看到源码的时候我们视乎感觉和上面的这个类差不多,唯一差别的就是最后一个的createMultipartConfig 返回的 MultipartConfigElement 对象。
MultipartConfigElement 就是最终的使用配置文件的类,再看源码我们看到就是有好几个构造方法,可以使用@MultipartConfig注解的方式等等。。。
三、配置文件加载以后,创建处理的组件。
对应自动配置类里的MultipartAutoConfiguration
这个方法的处理就是选择哪种Multipart 处理方式,默认情况下使用的是StandardServletMultipartResolver 。我们可以通过MultiparResolver来了解一下有哪些部件。
How To Set Spring.http.multipart.location For Macbook Pro
四、MultipartResolver介绍。
在springMVC中框架底层实际上也是使用了通用的早期的 jsp smart upload 和 Oreilly 的 COS 类库,以及用最多的 Commons FileUpload 类库,实际开发中,我们只需要使用这些专门针对表单的文件上传处理类库即可。
MultipartResolver 位于 HandlerMapping 之前,请求一来就交由它来处理。当 Web 请求到达 DispatcherServlet 并等待处理的时候,DispatcherServlet 首先会检查能否从自的 WebApplicationContext 中找到一个名称为 multipartResolver(由 DispatcherServlet 的常量 MULTIPART_RESOLVER_BEAN_NAME 所决定)的 MultipartResolver 实例。如果能够获得一个 MultipartResolver 的实例,DispatcherServlet 将调用 MultipartResolver 的 isMultipart(request) 方法检查当前 Web 请求是否为 multipart类型。如果是,DispatcherServlet 将调用 MultipartResolver 的 resolveMultipart(request) 方法,对原始 request 进行装饰,并返回一个 MultipartHttpServletRequest 供后继处理流程使用(最初的 HttpServletRequest 被偷梁换柱成了 MultipartHttpServletRequest),否则,直接返回最初的 HttpServletRequest。
需要特别说明的是MultipartResolver 只是一个接口,具体的实现SpringBoot给我们提供了CommonsMultipartResolver与StandardServletMultipartResolver两种处理方式(这里说的Springboot提供的处理方式,其实就是SpringMVC提供的)。
MultipartResolver 的 isMultipart(request) 方法好实现,当判断出当前的 request 是 multipart 类型的请求,它将调用 MultipartResolve 的 resolveMultipart(request)。这里的 request 就是原始的 HttpServletRequest 对象,奇迹就出现在这里。以 CommonsMultipartResolver 为例,当调用 resolveMultipart(request) 时,看看它是如何创建 MultipartRequest 的。 暂且不管 resolveLazily 为何意。假设 resolveLazily 为 false,我们看 else 的片段。由于是 CommonsMultipartResolver,它的 parseRequest 方法将从原始的 HttpServletRequest 中解析出文件,得到基于 Commons FileUpload API 的 FileItem 对象。Spring 在这里封装了一下,对于 MultipartResolver 而言,它看到的就是 MultipartFile。注意最后的 return,它将构建一个 DefaultMultipartHttpServletRequest,也就是 MultipartRequest。它将 MultipartFile 和 MultipartParameter 作为构造函数的参数传入,在这个构造函数里,有 setMultipartFiles 这句话。这个方法正是 AbstractMultipartHttpServletRequest 里的方法,这样,AbstractMultipartHttpServletRequest 的实例变量 multipartFiles 就有正规来源了吧,即解决了上面我们提到的疑问。然去实现 MultipartRequest 接口里的方法就是轻而易举的事了。
五、CommonsMultipartResolver与StandardServletMultipartResolve介绍。
StandardServletMultipartResolve为默认的实现来处理上传,将一个request包装成了一个StandardMultipartHttpServletRequest,这个类会使用parseRequest方法解析http报文,将上传文件封装成StandardMultipartFile挨个存储到MultiValueMap<String, MultipartFile>类型的map中并关联到处理后的request。
How To Set Spring.http.multipart.location For Macbook
StandardServletMultipartResolve是servlet3.0以后的容器才能使用。
CommonsMultipartResolver不会强制要求设置临时文件路径。默认情况下,这个路径就是Servlet容器的临时目录,我们也可以使用uploadTempDir属性来设置临时位置。
发表于 2017-03-10 10:10沙与沫 阅读(16438) 评论(0) 编辑收藏