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

网友投稿 292 2022-12-10

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

目录springboot文件上传保存路径配置代码如下Springboot上传文件的问题(上传到本地文件夹中)先建立一个controller包静态资源目录如下

springboot文件上传保存路径

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

http://@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,映射一个静态文件夹,用来存放上传的文件,这样在项目升级重新部署,文iMWni件依然存在。现在要解决的问题是,如何使用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},这个filehttp://是配置一个文件路径做作为静态资源映射,而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.texiMWnit.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文件比较简陋

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java最新版本连接mysql失败的解决过程
下一篇:基于SpringBoot启动类静态资源路径问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~