亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Springboot文件上傳出現(xiàn)找不到指定系統(tǒng)路徑的解決

 更新時(shí)間:2021年08月31日 15:20:44   作者:帶著希望活下去  
這篇文章主要介紹了Springboot文件上傳出現(xiàn)找不到指定系統(tǒng)路徑的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Springboot文件上傳出現(xiàn)找不到指定系統(tǒng)路徑

1、問題描述

關(guān)鍵字:SpringMVC 4.2.4、Spring Boot 1.3.1、Servlet 3.0、文件上傳

報(bào)錯(cuò)信息:

java.io.IOException: java.io.FileNotFoundException: /tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/tmp/source/IMG_20160129_132623.jpg (No such file or directory)

問題源碼:transferTo方法報(bào)錯(cuò)

// 前端傳入mulFileSource
// 創(chuàng)建壓縮前源文件
File fileSourcePath = new File("tmp/source/");
File fileSource = new File(fileSourcePath, mulFileSource.getOriginalFilename());
if (!fileSourcePath.exists()) {
    fileSourcePath.mkdirs();
}
// 將接收得圖片暫存到臨時(shí)文件中
mulFileSource.transferTo(fileSource);

2、問題分析

首先,看源碼中文件定義,相對路徑,預(yù)期路徑應(yīng)該是項(xiàng)目路徑/tmp/source/,但是報(bào)錯(cuò)確是一個(gè)系統(tǒng)臨時(shí)文件路徑(tomcat的)。

其次,由于是transferTo方法報(bào)錯(cuò),因此應(yīng)該是該方法寫入文件時(shí)報(bào)錯(cuò),因此,我們跟入方法源碼。

public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest {
//中間代碼省略
/**
 * Spring MultipartFile adapter, wrapping a Servlet 3.0 Part object.
 */
@SuppressWarnings("serial")
private static class StandardMultipartFile implements MultipartFile, Serializable {
//中間代碼省略
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
this.part.write(dest.getPath());
package org.apache.catalina.core;
/**
 * Adaptor to allow {@link FileItem} objects generated by the package renamed
 * commons-upload to be used by the Servlet 3.0 upload API that expects
 * {@link Part}s.
 */
public class ApplicationPart implements Part {
//中間代碼省略
    @Override
    public void write(String fileName) throws IOException {
        File file = new File(fileName);
        if (!file.isAbsolute()) {
            file = new File(location, fileName);
        }
        try {
            fileItem.write(file);
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
}

源碼一目了然,使用Servlet3.0的支持的上傳文件功能時(shí),如果我們沒有使用絕對路徑的話,transferTo方法會(huì)在相對路徑前添加一個(gè)location路徑,即:file = new File(location, fileName);。當(dāng)然,這也影響了SpringMVC的Multipartfile的使用。

由于我們創(chuàng)建的File在項(xiàng)目路徑/tmp/source/,而transferTo方法預(yù)期寫入的文件路徑為/tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/tmp/source/,我們并沒有創(chuàng)建該目錄,因此會(huì)拋出異常。

3、問題解決方案

使用絕對路徑

修改location的值

這個(gè)location可以理解為臨時(shí)文件目錄,我們可以通過配置location的值,使其指向我們的項(xiàng)目路徑,這樣就解決了我們遇到的問題。

在Spring Boot下配置location,可以在main()方法所在文件中添加如下代碼:

/**
 * 文件上傳臨時(shí)路徑
 */
 @Bean
 MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setLocation("/app/pttms/tmp");
    return factory.createMultipartConfig();
}

SpringBoot 上傳文件時(shí)本地路徑無效

SpringBoot 通過網(wǎng)關(guān)Zuul進(jìn)行附件上傳的時(shí)候,有時(shí)會(huì)出現(xiàn)如下錯(cuò)誤

[Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io .IOException: The temporary upload location [/tmp/tomcat.3814974221022613431.8080/work/Tomcat/localhost/ROOT] is not valid] with root causejava.io .IOException: The temporary upload location [/tmp/tomcat.3814974221022613431.8080/work/Tomcat/localhost/ROOT] is not valid

錯(cuò)誤產(chǎn)生的原因

1、spring boot的應(yīng)用服務(wù)在啟動(dòng)的時(shí)候,會(huì)生成在操作系統(tǒng)的/tmp目錄下生成一個(gè)Tomcat.*的文件目錄,用于"java.io.tmpdir"文件流操作

TomcatEmbeddedServletContainerFactory

2、程序?qū)ξ募牟僮鲿r(shí):會(huì)生成臨時(shí)文件,暫存在臨時(shí)文件中;長時(shí)間不操作,導(dǎo)致/tmp下面的tomcat臨時(shí)文件目錄被刪除,

且刪除的文件不可恢復(fù),上傳文件時(shí)獲取不到文件目錄,報(bào)錯(cuò)

解決方式有以下幾點(diǎn)

1.重啟服務(wù);

2.網(wǎng)關(guān)是否引入spring-boot-starter-web依賴,若無,則將其引入;

3、設(shè)置spring.servlet.multipart.enabled:true

4.修改上傳文件默認(rèn)的BaseDir

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論