Spring Boot实现文件上传和下载

发布时间 2023-12-04 16:33:11作者: Xproer-松鼠

实现Spring Boot文件上传和下载的步骤:
1.文件上传
在pom.xml文件中添加依赖:spring-boot-starter-web和spring-boot-starter-thymeleaf。
创建一个上传前端的页面,包括一个表单来选择文件和一个提交按钮。
在Controller中添加一个POST方法,该方法接受MultipartFile参数,将文件保存在服务器上。
在application.properties文件中配置上传文件的最大大小和文件存储路径。
2.文件下载
在Controller中添加一个GET方法,该方法接收一个文件名参数,通过ResponseEntity将文件内容返回给客户端。
如果需要授权访问,则可以通过使用Spring Security实现基本身份验证。
以下是Spring Boot实现文件上传和下载的代码示例:

1.文件上传
pom.xml依赖配置:
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>

<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

在配置文件中配置:
# 开启上传和下载
spring.servlet.multipart.enabled=true
# 最大的文件大小
spring.servlet.multipart.max-file-size=30MB
# 单次最大请求大小
spring.servlet.multipart.max-request-size=30MB
# 自定义的上传文件存放路径
file.upload.dir=e:/test

在Controller中添加一个POST方法:
@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "上传失败,请选择文件";
}

// 获取文件名
String fileName = file.getOriginalFilename();
// 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));

// 上传后的路径
String filePath = "D:/upload/";

// 解决中文问题,liunx下中文路径,图片显示问题
fileName = UUID.randomUUID() + suffixName;

// 构建上传路径
File dest = new File(filePath + fileName);

// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}

try {
// 保存文件
file.transferTo(dest);
return "上传成功";
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败";
}


2.文件下载
在Controller中添加一个GET方法:
@GetMapping("/download")
public ResponseEntity<byte[]> download() throws IOException {
// 获取文件
File file = new File("D:/upload/1.jpg");

// 构造响应
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", file.getName());
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}

编写Controller方法
@GetMapping("/download")
public void downloadFile(HttpServletResponse response) {
try {
//获取要下载的文件
File file = new File("file_path");

//设置响应的内容类型为二进制流,即文件类型
response.setContentType("application/octet-stream");

//设置文件名
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));

//创建输入流
FileInputStream inputStream = new FileInputStream(file);
BufferedInputStream buffInputStream = new BufferedInputStream(inputStream);

//创建输出流
ServletOutputStream outputStream = response.getOutputStream();
BufferedOutputStream buffOutputStream = new BufferedOutputStream(outputStream);

//循环读取数据并写入到响应输出流中
byte[] buffer = new byte[1024];
int len = -1;
while ((len = buffInputStream.read(buffer)) != -1) {
buffOutputStream.write(buffer, 0, len);
}

//关闭流
buffOutputStream.flush();
buffOutputStream.close();
outputStream.flush();
outputStream.close();
buffInputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}


在SpringBoot中,我们可以通过以下步骤实现文件响应给浏览器实现下载:

在Controller中编写请求处理方法,并使用@GetMapping注解来指定该方法处理GET请求。

在方法中使用InputStreamResource将本地文件以流的形式读取到内存中。

使用ResponseEntity.ok()方法构造响应,并将之前获取到的InputStreamResource作为参数传入。

使用.header()方法设置响应头,包括Content-Disposition、Content-Type等信息。

以下是一个示例代码:

import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class FileDownloadController {

@GetMapping("/download")
public ResponseEntity<InputStreamResource> downloadFile() throws IOException {
String filename = "example.txt";
InputStream is = new FileInputStream(filename);
InputStreamResource resource = new InputStreamResource(is);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=\"" + filename + "\"");
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");

return ResponseEntity.ok()
.headers(headers)
.contentLength(is.available())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
}
}
其中,Content-Disposition头用于描述响应结果的类型,这里指定为attachment表示要下载的文件,filename表示文件名。

Cache-Control、Pragma、Expires头用于控制缓存的行为,这里设为不缓存。

最后将InputStreamResource对象作为响应体返回即可。

 

参考文章:http://blog.ncmem.com/wordpress/2023/12/04/spring-boot%e5%ae%9e%e7%8e%b0%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e5%92%8c%e4%b8%8b%e8%bd%bd/

欢迎入群一起讨论