当前位置 博文首页 > 文章内容

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

    作者:shunshunshun18 栏目:未分类 时间:2021-09-09 14:43:42

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    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文件比较简陋

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持IIS7站长之家博文。