本例子基于spring-boot-2.1.6.RELEASE开发
pom文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--spring-boot-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
springboot对于文件上传的yml配置
spring:
servlet:
multipart:
max-file-size: 2MB #上传一个文件最大值 默认是1MB
max-request-size: 20MB #上传多个文件最大值 默认是10MB
直接上代码
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
@RestController
@RequestMapping(path = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class FileController {
final String path = "D:\\tmp\\";
final String fileName = "D:\\tmp\\你好.txt";
//文件上传
@PostMapping(path = "/upload")
public String upload(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
//获取原始文件名
String originalFilename = file.getOriginalFilename();
File localFile = new File(path + originalFilename);
try {
//直接将文件拷贝到指定位置
file.transferTo(localFile);
} catch (IOException e) {
e.printStackTrace();
}
if (localFile.exists()) {
return "ok";
} else {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return "error";
}
}
//批量文件上传
@PostMapping(path = "/uploads")
public String uploadFiles(@RequestParam("file") MultipartFile[] files, HttpServletResponse response) {
boolean result = true;
StringBuilder stringBuilder = new StringBuilder();
for (MultipartFile file : files) {
//获取原始文件名
String originalFilename = file.getOriginalFilename();
File localFile = new File(path + originalFilename);
try {
//直接将文件拷贝到指定位置
file.transferTo(localFile);
} catch (IOException e) {
e.printStackTrace();
}
boolean exists = localFile.exists();
if (!exists) {
result = false;
stringBuilder.append(file.getOriginalFilename()).append(" ");
}
}
if (result) {
return "ok";
}
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return "upload fail, file name : " + stringBuilder.toString();
}
//文件下载
@GetMapping("/download")
public void downloadFile(HttpServletResponse response) {
File file = new File(fileName);
if (file.exists()) {
try (FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis)) {
response.setContentType("application/force-download");
//保证下载时文件名为指定文件名,避免中文文件名乱码
response.addHeader("Content-Disposition", "attachment;fileName="
+ URLEncoder.encode(file.getName(), "utf-8"));
OutputStream os = response.getOutputStream();
StreamUtils.copy(bis, os);
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
e.printStackTrace();
}
}
}
}
测试上传单个文件
测试上传多个文件
下载文件测试直接使用浏览器请求接口URL即可
对于通过SpringCloudZuul映射后的文件上传请求,在文件上传时请求的url中需要加上/zuul前缀才能保证中文不乱码