這篇文章主要為大家展示了“Spring Boot如何實(shí)現(xiàn)文件上傳”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Spring Boot如何實(shí)現(xiàn)文件上傳”這篇文章吧。
10年積累的成都網(wǎng)站建設(shè)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有鄂爾多斯免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
首先我們要知道什么是Spring Boot,這里簡(jiǎn)單說(shuō)一下,Spring Boot可以看作是一個(gè)框架中的框架--->集成了各種框架,像security、jpa、data、cloud等等,它無(wú)須關(guān)心配置可以快速啟動(dòng)開發(fā),有興趣可以了解下自動(dòng)化配置實(shí)現(xiàn)原理,本質(zhì)上是 spring 4.0的條件化配置實(shí)現(xiàn),深拋下注解,就會(huì)看到了。
說(shuō)Spring Boot 文件上傳原理 其實(shí)就是Spring MVC,因?yàn)檫@部分工作是Spring MVC做的而不是Spring Boot,那么,SpringMVC又是怎么處理文件上傳這個(gè)過程的呢?
圖:
首先項(xiàng)目啟動(dòng)相關(guān)配置,再執(zhí)行上述第二步的時(shí)候 DispatcherServlet會(huì)去查找id為multipartResolver的Bean,在配置中看到Bean指向的是CommonsMultipartResolve,其中實(shí)現(xiàn)了MultipartResolver接口。
第四步驟這里會(huì)判斷是否multipart文件即isMultipart方法,返回true:就會(huì)調(diào)用 multipartResolver 方法,傳遞HttpServletRequest會(huì)返回一個(gè)MultipartHttpServletRequest對(duì)象,再有DispatcherServlet進(jìn)行處理到Controller層;返回false:會(huì)忽略掉,繼續(xù)傳遞HttpServletRequest。
在MVC中需要在配置文件webApplicationContext.xml中配置 如下:
而Spring Boot已經(jīng)自動(dòng)配置好,直接用就行,做個(gè)test沒什么問題。有默認(rèn)的上傳限制大小,不過在實(shí)際開發(fā)中我們還是做一些配置的,
如下在application.properties中:
# multipart config #默認(rèn)支持文件上傳 spring.http.multipart.enabled=true #文件上傳目錄 spring.http.multipart.location=/tmp/xunwu/images/ #最大支持文件大小 spring.http.multipart.max-file-size=4Mb #最大支持請(qǐng)求大小 spring.http.multipart.max-request-size=20MB
當(dāng)然也可以寫配置類來(lái)實(shí)現(xiàn),具體的就不做展示了。
看完上述你肯定有個(gè)大概的了解了,這里再啰嗦下,Spring提供Multipart的解析器:MultipartResolver,上述說(shuō)的是CommonsMultipartResolver,它是基于Commons File Upload第三方來(lái)實(shí)現(xiàn),這也是在Servlet3.0之前的東西,3.0+之后也可以不需要依賴第三方庫(kù),可以用StandardServletMultipartResolver,同樣也是實(shí)現(xiàn)了MultipartResolver接口,我們可以看下它的實(shí)現(xiàn):
* Copyright 2002-2017 the original author or authors. package org.springframework.web.multipart.support; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Part; import org.apache.commons.logging.LogFactory; import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartResolver; /** * Standard implementation of the {@link MultipartResolver} interface, * based on the Servlet 3.0 {@link javax.servlet.http.Part} API. * To be added as "multipartResolver" bean to a Spring DispatcherServlet context, * without any extra configuration at the bean level (see below). * *Note: In order to use Servlet 3.0 based multipart parsing, * you need to mark the affected servlet with a "multipart-config" section in * {@code web.xml}, or with a {@link javax.servlet.MultipartConfigElement} * in programmatic servlet registration, or (in case of a custom servlet class) * possibly with a {@link javax.servlet.annotation.MultipartConfig} annotation * on your servlet class. Configuration settings such as maximum sizes or * storage locations need to be applied at that servlet registration level; * Servlet 3.0 does not allow for them to be set at the MultipartResolver level. * * @author Juergen Hoeller * @since 3.1 * @see #setResolveLazily * @see HttpServletRequest#getParts() * @see org.springframework.web.multipart.commons.CommonsMultipartResolver */ public class StandardServletMultipartResolver implements MultipartResolver { private boolean resolveLazily = false; /** * Set whether to resolve the multipart request lazily at the time of * file or parameter access. *
Default is "false", resolving the multipart elements immediately, throwing * corresponding exceptions at the time of the {@link #resolveMultipart} call. * Switch this to "true" for lazy multipart parsing, throwing parse exceptions * once the application attempts to obtain multipart files or parameters. */ public void setResolveLazily(boolean resolveLazily) { this.resolveLazily = resolveLazily; } @Override public boolean isMultipart(HttpServletRequest request) { // Same check as in Commons FileUpload... if (!"post".equals(request.getMethod().toLowerCase())) { return false; } String contentType = request.getContentType(); return (contentType != null && contentType.toLowerCase().startsWith("multipart/")); } @Override public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException { return new StandardMultipartHttpServletRequest(request, this.resolveLazily); } @Override public void cleanupMultipart(MultipartHttpServletRequest request) { // To be on the safe side: explicitly delete the parts, // but only actual file parts (for Resin compatibility) try { for (Part part : request.getParts()) { if (request.getFile(part.getName()) != null) { part.delete(); } } } catch (Throwable ex) { LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex); } } }
這里是之前寫的test的后者實(shí)現(xiàn)配置類,可以簡(jiǎn)單看下,作為了解:
package com.bj.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.web.MultipartProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.MultipartConfigElement; @Configuration @EnableConfigurationProperties(MultipartProperties.class) public class FileUploadConfig { private final MultipartProperties multipartProperties; public FileUploadConfig(MultipartProperties multipartProperties){ this.multipartProperties=multipartProperties; } /** * 注冊(cè)解析器 * @return */ @Bean(name= DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) @ConditionalOnMissingBean(MultipartResolver.class) public StandardServletMultipartResolver multipartResolver(){ StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver(); multipartResolver.setResolveLazily(multipartProperties.isResolveLazily()); return multipartResolver; } /** * 上傳配置 * @return */ @Bean @ConditionalOnMissingBean public MultipartConfigElement multipartConfigElement(){ return this.multipartProperties.createMultipartConfig(); } }
以上是“Spring Boot如何實(shí)現(xiàn)文件上傳”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!