Spring Boot應(yīng)用上傳文件時報錯的原因及解決方案
問題描述
Spring Boot應(yīng)用(使用默認(rèn)的嵌入式Tomcat)在上傳文件時,偶爾會出現(xiàn)上傳失敗的情況,后臺報錯日志信息如下:“The temporary upload location is not valid”。
原因追蹤
這個問題的根本原因是Tomcat的文件上傳機制引起的!
Tomcat在處理文件上傳時,會將客戶端上傳的文件寫入臨時目錄,這個臨時目錄默認(rèn)在/tmp路徑下,如:“/tmp/tomcat.6574404581312272268.18333/work/Tomcat/localhost/ROOT”。
而操作系統(tǒng)對于/tmp目錄會不定時進(jìn)行清理,如果正好因為操作系統(tǒng)的清理導(dǎo)致對應(yīng)的臨時目錄被刪除,客戶端再上傳文件時就會報錯:“The temporary upload location is not valid”。
實際上,追蹤一下源碼會發(fā)現(xiàn),如果不明確設(shè)置Tomcat的文件上傳臨時目錄,默認(rèn)讀取的是Servlet上下文對象的屬性“javax.servlet.context.tempdir”值,如下源碼:
- org.apache.catalina.connector.Request
private void parseParts(boolean explicit) { //... MultipartConfigElement mce = this.getWrapper().getMultipartConfigElement(); //... // 讀取MultipartConfigElement對象的location屬性 String locationStr = mce.getLocation(); File location; if (locationStr != null && locationStr.length() != 0) { location = new File(locationStr); if (!location.isAbsolute()) { location = (new File((File)context.getServletContext().getAttribute("javax.servlet.context.tempdir"), locationStr)).getAbsoluteFile(); } } else { // 如果location屬性值為空,則讀取Servlet上下文對象的屬性“javax.servlet.context.tempdir”值(如:/tmp/tomcat.6574404581312272268.18333/work/Tomcat/localhost/ROOT) location = (File)context.getServletContext().getAttribute("javax.servlet.context.tempdir"); } //... }
解決辦法
既然是因為上傳文件的臨時路徑被刪除導(dǎo)致的問題,就要確保改臨時目錄不會被刪除。
2種解決方法:
(1)通過Spring Boot的配置參數(shù)“spring.servlet.multipart.location”明確指定上傳文件的臨時目錄,確保該路徑已經(jīng)存在,而且該目錄不會被操作系統(tǒng)清除。
spring.servlet.multipart.location=/data/tmp
如上所示,將上傳文件的臨時目錄指定到路徑“/data/tmp”下。
實際上,在Spring Boot中關(guān)于上傳文件的所有配置參數(shù)如下所示:
# MULTIPART (MultipartProperties) spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads. spring.servlet.multipart.file-size-threshold=0B # Threshold after which files are written to disk. spring.servlet.multipart.location= # Intermediate location of uploaded files. spring.servlet.multipart.max-file-size=1MB # Max file size. spring.servlet.multipart.max-request-size=10MB # Max request size. spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.
(2)在Spring容器中明確注冊MultipartConfigElement對象,通過MultipartConfigFactory指定一個路徑。
在上述源碼追蹤中就發(fā)現(xiàn),Tomcat會使用MultipartConfigElement
對象的location屬性作為上傳文件的臨時目錄。
/** * 配置上傳文件臨時目錄 * @return */ @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // tmp.dir參數(shù)在啟動腳本中設(shè)置 String path = System.getProperty("tmp.dir"); if(path == null || "".equals(path.trim())) { path = System.getProperty("user.dir"); } String location = path + "/tmp"; File tmpFile = new File(location); // 如果臨時目錄不存在則創(chuàng)建 if (!tmpFile.exists()) { tmpFile.mkdirs(); } // 明確指定上傳文件的臨時目錄 factory.setLocation(location); return factory.createMultipartConfig(); }
參考
以上就是Spring Boot應(yīng)用上傳文件時報錯的原因及解決方案的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot應(yīng)用上傳文件時報錯的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java找出兩個大數(shù)據(jù)量List集合中的不同元素的方法總結(jié)
本文將帶大家了解如何快速的找出兩個相似度非常高的List集合里的不同元素。主要通過Java API、List集合雙層遍歷比較不同、借助Map集合查找三種方式,需要的可以參考一下2022-10-10Java零基礎(chǔ)也看得懂的單例模式與final及抽象類和接口詳解
本文主要講了單例模式中的餓漢式和懶漢式的區(qū)別,final的使用,抽象類的介紹以及接口的具體內(nèi)容,感興趣的朋友來看看吧2022-05-05java實現(xiàn)哈弗曼編碼與反編碼實例分享(哈弗曼算法)
本文介紹java實現(xiàn)哈弗曼編碼與反編碼實例,大家參考使用吧2014-01-01淺談spring中用到的設(shè)計模式及應(yīng)用場景
下面小編就為大家?guī)硪黄獪\談spring中用到的設(shè)計模式及應(yīng)用場景。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08