当前位置:实例文章 » JAVA Web实例» [文章]SpringBoot文件上传-单文件与多文件上传的使用

SpringBoot文件上传-单文件与多文件上传的使用

发布人:shili8 发布时间:2025-02-21 04:07 阅读次数:0

**Spring Boot 文件上传**

在实际开发中,文件上传是非常常见的需求。Spring Boot 提供了一个简单易用的 API 来处理文件上传。下面我们将介绍如何实现单文件与多文件上传。

### 单文件上传首先,我们需要在 Spring Boot项目中添加必要的依赖:

xml<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
 <groupId>commons-io</groupId>
 <artifactId>commons-io</artifactId>
 <version>2.8.0</version>
</dependency>


接下来,我们需要创建一个 Controller 来处理文件上传:

javaimport org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet. />import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

@RestControllerpublic class FileController {

 @PostMapping("/upload")
 public String upload(@RequestParam("file") MultipartFile file) {
 //保存文件到本地 String filePath = "/path/to/upload";
 try {
 File targetFile = new File(filePath, file.getOriginalFilename());
 file.transferTo(targetFile);
 return "上传成功!";
 } catch (IOException e) {
 return "上传失败!" + e.getMessage();
 }
 }

 @GetMapping("/download")
 public void download(HttpServletResponse response) {
 // 下载文件 String filePath = "/path/to/download";
 try {
 File file = new File(filePath);
 FileInputStream in = new FileInputStream(file);
 OutputStream out = response.getOutputStream();
 response.setContentType("application/octet-stream");
 response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
 byte[] buffer = new byte[1024];
 int len;
 while ((len = in.read(buffer)) != -1) {
 out.write(buffer,0, len);
 }
 in.close();
 out.flush();
 out.close();
 } catch (IOException e) {
 System.out.println(e.getMessage());
 }
 }
}


### 多文件上传多文件上传与单文件上传类似,但是需要处理多个 MultipartFile 对象:

javaimport org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet. />import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

@RestControllerpublic class FileController {

 @PostMapping("/upload")
 public String upload(@RequestParam("files") MultipartFile[] files) {
 //保存文件到本地 String filePath = "/path/to/upload";
 try {
 for (MultipartFile file : files) {
 File targetFile = new File(filePath, file.getOriginalFilename());
 file.transferTo(targetFile);
 }
 return "上传成功!";
 } catch (IOException e) {
 return "上传失败!" + e.getMessage();
 }
 }

 @GetMapping("/download")
 public void download(HttpServletResponse response) {
 // 下载文件 String filePath = "/path/to/download";
 try {
 File file = new File(filePath);
 FileInputStream in = new FileInputStream(file);
 OutputStream out = response.getOutputStream();
 response.setContentType("application/octet-stream");
 response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
 byte[] buffer = new byte[1024];
 int len;
 while ((len = in.read(buffer)) != -1) {
 out.write(buffer,0, len);
 }
 in.close();
 out.flush();
 out.close();
 } catch (IOException e) {
 System.out.println(e.getMessage());
 }
 }
}


### 总结在本文中,我们介绍了如何使用 Spring Boot 实现单文件与多文件上传。通过使用 MultipartFile 对象,可以轻松地处理文件上传和下载需求。

相关标签:java前端开发语言
其他信息

其他资源

Top