温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何解决springboot文件上传保存路径的问题

发布时间:2021-09-09 14:53:09 来源:亿速云 阅读:365 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关如何解决springboot文件上传保存路径的问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

springboot文件上传保存路径

最近使用springboot整合富文本编辑器wangeditor,在整合的时候,对于图片上传时候保存路径出现了一些问题,代码如下

@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;
    }

报了如下异常

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

我到了相应路径下查看,路径下确实没有这些文件,因为springboot直接以jar包的方式直接运行,应该是jar包运行时生成的相关路径,并没有深入研究原理。这里并不像tomcat下部署那样,文件夹确实存在,并且可读取。所以只能采用其他方案。

在我们在tomcat下做文件上传时候,我们也并不会直接在项目下面的相关路径下保存上传的文件,因为如果这样保存,项目重新部署,文件就没了。通常是配置tomcat,映射一个静态文件夹,用来存放上传的文件,这样在项目升级重新部署,文件依然存在。现在要解决的问题是,如何使用springboot配置静态文件夹。

配置代码如下

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

关键的代码就是file:${web.upload-path},这个file是配置一个文件路径做作为静态资源映射,而upload-path: F:/Java/upload/是配置路径的实际位置。其余的代码,因为自己重新配置静态资源映射,所以默认配置失效,自己要重新配置一下。

当然除了配置文件配置,还可以自己继承WebMvcConfigurerAdapter类来配置静态资源映射,当然这是低版本的springboot,高版本的继承WebMvcConfigurerationSupport实现。

Springboot上传文件的问题(上传到本地文件夹中)

先建立一个controller包

如图所示:

如何解决springboot文件上传保存路径的问题

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 "上传失败!";
    }
}

这里我们是上传在了默认文件夹里面,

我的是

如何解决springboot文件上传保存路径的问题

这个文件夹下。

当然你也可以根据自己的意愿放在自己想要的文件下面,此时需要改变以上的一点代码():

//file 这里是我自己想要的文件夹
 String file = "E:\\IDEA01\\学习springboot\\springboot_uploadfiles\\src\\main\\resources\\static\\uploadFile";
 File folder = new File(file);

此时会出现上传的文件

如何解决springboot文件上传保存路径的问题

静态资源目录如下

如何解决springboot文件上传保存路径的问题

因为主要是演示上传文件,所以HTML文件比较简陋

关于“如何解决springboot文件上传保存路径的问题”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI