溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何解決springboot文件上傳保存路徑的問(wèn)題

發(fā)布時(shí)間:2021-09-09 14:53:09 來(lái)源:億速云 閱讀:390 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何解決springboot文件上傳保存路徑的問(wèn)題,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

springboot文件上傳保存路徑

最近使用springboot整合富文本編輯器wangeditor,在整合的時(shí)候,對(duì)于圖片上傳時(shí)候保存路徑出現(xiàn)了一些問(wèn)題,代碼如下

@PostMapping("/upload")
    public Result upload(MultipartFile[] file, HttpServletRequest request) throws IOException {
        Result result = new Result();        
        String path = request.getServletContext().getRealPath("/public/image/upload");
        System.out.println(file.length);
        for (MultipartFile f : file) {
            String name = f.getOriginalFilename();
            f.transferTo(new File(path+File.separator+name));
            result.getData().add("/public/image/upload/"+name);
        }
        return result;
    }

報(bào)了如下異常

java.io.IOException: java.io.FileNotFoundException: C:\Users\22374\AppData\Local\Temp\
tomcat-docbase.262090075120668420.9003\public\image\upload\demo0.png (系統(tǒng)找不到指定的路徑。)

我到了相應(yīng)路徑下查看,路徑下確實(shí)沒(méi)有這些文件,因?yàn)閟pringboot直接以jar包的方式直接運(yùn)行,應(yīng)該是jar包運(yùn)行時(shí)生成的相關(guān)路徑,并沒(méi)有深入研究原理。這里并不像tomcat下部署那樣,文件夾確實(shí)存在,并且可讀取。所以只能采用其他方案。

在我們?cè)趖omcat下做文件上傳時(shí)候,我們也并不會(huì)直接在項(xiàng)目下面的相關(guān)路徑下保存上傳的文件,因?yàn)槿绻@樣保存,項(xiàng)目重新部署,文件就沒(méi)了。通常是配置tomcat,映射一個(gè)靜態(tài)文件夾,用來(lái)存放上傳的文件,這樣在項(xiàng)目升級(jí)重新部署,文件依然存在。現(xiàn)在要解決的問(wèn)題是,如何使用springboot配置靜態(tài)文件夾。

配置代碼如下

spring:
  mvc:
    static-path-pattern: /**
  resources:
    static-locations:
    - classpath:/META-INF/resources/
    - classpath:/static
    - classpath:/resources/
    - file:${upload-path}
upload-path: F:/Java/upload/

關(guān)鍵的代碼就是file:${web.upload-path},這個(gè)file是配置一個(gè)文件路徑做作為靜態(tài)資源映射,而upload-path: F:/Java/upload/是配置路徑的實(shí)際位置。其余的代碼,因?yàn)樽约褐匦屡渲渺o態(tài)資源映射,所以默認(rèn)配置失效,自己要重新配置一下。

當(dāng)然除了配置文件配置,還可以自己繼承WebMvcConfigurerAdapter類(lèi)來(lái)配置靜態(tài)資源映射,當(dāng)然這是低版本的springboot,高版本的繼承WebMvcConfigurerationSupport實(shí)現(xiàn)。

Springboot上傳文件的問(wèn)題(上傳到本地文件夾中)

先建立一個(gè)controller包

如圖所示:

如何解決springboot文件上傳保存路徑的問(wèn)題

FileUploadController.java代碼如下所示:

package com.zcc.springboot_uploadfiles.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@RestController
public class FileUploadController {
    SimpleDateFormat sdf = new SimpleDateFormat("yyy/MM/dd/");
    @PostMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest req) {
        String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
        String format = sdf.format(new Date());
        String file = realPath + format;
        File folder = new File(realPath + format);
        if(!folder.isDirectory()) {
            folder.mkdirs();
        }
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString() +
                oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            // 文件保存操作
            uploadFile.transferTo(new File(folder, newName));
            return file;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上傳失敗!";
    }
}

這里我們是上傳在了默認(rèn)文件夾里面,

我的是

如何解決springboot文件上傳保存路徑的問(wèn)題

這個(gè)文件夾下。

當(dāng)然你也可以根據(jù)自己的意愿放在自己想要的文件下面,此時(shí)需要改變以上的一點(diǎn)代碼():

//file 這里是我自己想要的文件夾
 String file = "E:\\IDEA01\\學(xué)習(xí)springboot\\springboot_uploadfiles\\src\\main\\resources\\static\\uploadFile";
 File folder = new File(file);

此時(shí)會(huì)出現(xiàn)上傳的文件

如何解決springboot文件上傳保存路徑的問(wèn)題

靜態(tài)資源目錄如下

如何解決springboot文件上傳保存路徑的問(wèn)題

因?yàn)橹饕茄菔旧蟼魑募訦TML文件比較簡(jiǎn)陋

關(guān)于“如何解決springboot文件上傳保存路徑的問(wèn)題”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI