springboot文件上传保存路径 springboot文件上传保存路径的问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot文件上传保存路径 springboot文件上传保存路径的问题

include_cpp   2021-09-09 我要评论
想了解springboot文件上传保存路径的问题的相关内容吗,include_cpp在本文为您仔细讲解springboot文件上传保存路径的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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包

如图所示:

在这里插入图片描述

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

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

我的是

在这里插入图片描述

这个文件夹下。

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

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

此时会出现上传的文件

在这里插入图片描述

静态资源目录如下

在这里插入图片描述

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们