springboot文件上传下载的简单实现
说明
记录每天的学习情况,最近在回顾程序员青戈讲的知识点,B站,csdn都有他讲解的内容…
spring,springMVC常见的注解以及知识点的回顾
1.注解
@RequestParam
/**
* 方式三 使用@RequestParam注解实现
* RequestParam注解的所需的参数必须和请求数据的键一致,根据该参数获取请求的值并写入对应修饰的参数位置
* @param name
* @param pwd
* @return
*/
@RequestMapping("demo5")
public String demo5(@RequestParam("userName") String name, @RequestParam("password") String pwd){
System.out.println(name);
System.out.println(pwd);
return "success";
}
@value
常用的三种方式
(1) @Value(“value值’')直接把字符串(value值)直接赋值给当前字符串
(2) @Value(“#{lx}”)先解析spring表达式,将结果赋值给当前字段(解析spring表达式的结果可能是字符串或者是一个Bean对象)
(3) @Value(”${lx}"} 先进行占位符的替换,然后将替换后的字符串赋值给当前的字符段; 根据操作系统变量,jvm环境变量,properties文件作为替换的来源
2.解决乱码问题
解决上传文件名乱码问题
response.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileUUID,"UTF-8"));
客户端的浏览器区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
response.setContentType("application/octet-stream");
文件上传的实现
我所使用的一些工具类
1. hutool
poi-ooxml是与excel相关的
<!-- hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.20</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
数据库的创建
id主键、name文件名称、type文件类型、size文件大小、url文件存放路径、is_delete假删除、enable是否启用文件
具体实现
文件和下载
@RestController
@RequestMapping("file")
public class FileController {
@Resource
private FilesService filesService;
@Value("${files.upload.path}")
private String uploadPath;
/**
* 文件上传
* @param file
* @return
*/
@PostMapping("upload")
public String uploadFile(@RequestParam MultipartFile file){
String url = null;
try {
//获取源文件的名称
String originalFilename = file.getOriginalFilename();
//利用hutool工具类获取文件类型
String type = FileUtil.extName(originalFilename);
//文件大小
long size = file.getSize();
//创建File类
//利用hutool工具类创建文件的唯一标识码
String uuid = IdUtil.fastSimpleUUID();
String fileUUid = uuid + "." + type;
File uploadFile = new File(uploadPath + fileUUid);
//判断上传文件的父级文件目录是否存在,若不存在则创建一个新的文件目录
File parentFile = uploadFile.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
//存储到磁盘目录
file.transferTo(uploadFile);
//获取文件的MD5(文件的md5是为了避免上传相同的文件)
String md5 = SecureUtil.md5(uploadFile);
//查询文件的md5是否存在(2个文件是否相同)
Files fileByMd5 = filesService.findFileByMd5(md5);
if(fileByMd5 != null){
url = fileByMd5.getUrl();
//文件已经在本地磁盘存在,删除刚刚上传的重复文件
uploadFile.delete();
}else{
url = "http://localhost:8080/file/download/" + fileUUid;
}
//存储到数据库中
Files saveFile = new Files();
saveFile.setName(originalFilename);
saveFile.setType(type);
saveFile.setSize(size/1024);//以KB为单位
saveFile.setUrl(url);
saveFile.setMd5(md5);
filesService.insertFile(saveFile);
} catch (IOException e) {
e.printStackTrace();
}
//存储到数据库
return url;
}
/**
* 文件下载
* @param fileUUID
* @param response
*/
@GetMapping("download/{fileUUID}")
public void downloadFile(@PathVariable("fileUUID") String fileUUID, HttpServletResponse response){
try {
// 根据文件的唯一标识获取文件
File uploadFile = new File(uploadPath + fileUUID);
// 设置输出流的格式
ServletOutputStream os = response.getOutputStream();
response.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileUUID,"UTF-8"));
response.setContentType("application/octet-stream");
// 读取文件的字符流
os.write(FileUtil.readBytes(uploadFile));
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
yml中根据情况设置
files:
upload:
path: D:/uploadTest/