导入依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
编写配置yml文件
spring:
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 100MB
file-size-threshold: 512KB
location: /
编写配置类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import javax.servlet.MultipartConfigElement;
/**
* @author およそ神
* @version JDK 1.8
*/
@Configuration
public class UploadConfig {
@Value("${spring.servlet.multipart.max-file-size}")
private String maxFile;
@Value("${spring.servlet.multipart.max-request-size}")
private String maxSize;
@Value("${spring.servlet.multipart.location}")
private String location;
@Bean
public MultipartConfigElement getMultipartConfig() {
MultipartConfigFactory config = new MultipartConfigFactory();
// 设置上传文件的单个大小限制
config.setMaxFileSize(DataSize.parse(maxFile));
// 设置总的上传的大小限制
config.setMaxRequestSize(DataSize.parse(maxSize));
// 设置临时保存目录
config.setLocation(location);
// 创建一个上传配置并返回
return config.createMultipartConfig();
}
}
编写controller
/**
* 文件上传
* @param file 上传文件
* @return
*/
@PostMapping("/upload")
public String upload(@RequestBody MultipartFile file) {
if(file.isEmpty()){
return "文件为空";
}
System.out.println("文件的大小为 :" + file.getSize());
System.out.println("文件的媒体类型为 : " + file.getContentType());
System.out.println("文件名为: " + file.getOriginalFilename());
// 设置文件存储路径
String filePath = loadPath;
String uploadFilePath = uploadFile(file, filePath);
if (!StrUtil.isBlank(uploadFilePath)){
return "上传成功: "+uploadFilePath;
}
return "上传失败";
}
/**
* 上传多个文件
* @param request
* @return
*/
@PostMapping("/uploadBatch01")
public String uploadBatch01(@RequestBody MultipartHttpServletRequest request){
List<MultipartFile> files = request.getFiles("file");
String filePath = loadPath;
for (int i = 0; i < files.size(); i++){
MultipartFile file = files.get(i);
if (!file.isEmpty()){
uploadFile(file, filePath);
System.out.println("第 "+i+" 个文件上传成功");
}else {
System.out.println("第 "+i+" 个文件上传失败");
}
}
return "上传成功";
}
private static String filePath = "C:\\Users\\Administrator\\Desktop\\嘤嘤乖\\";
private static String loadPath = "C:\\Users\\Administrator\\Desktop";
/**
* 上传多个文件
* @param
* @return
*/
@PostMapping("/uploadBatch02")
public boolean uploadBatch02(@RequestBody MultipartFile[] files) throws IllegalStateException, IOException {
if(files.length == 0) {
return false;
}
File file = null;
String filePath = "C:\\Users\\Administrator\\Desktop";
for (int i = 0; i < files.length; i++) {
System.out.println("第" + i + "个文件的大小为" + files[i].getSize());
System.out.println("第" + i + "个文件是否为空" + files[i].isEmpty());
System.out.println("第" + i + "个文件的媒体类型为" + files[i].getContentType());
System.out.println("第" + i + "个文件的文件名为" + files[i].getName());
System.out.println("第" + i + "个文件的源文件名为" + files[i].getOriginalFilename());
file = new File(filePath + files[i].getOriginalFilename());
files[i].transferTo(file);
}
return false;
}
private static String fileName = "0b25b97a8a674a2174389af57e22622.jpg";
/**
* 文件下载
* @param response
* @return
*/
@GetMapping("/download")
public void download(HttpServletResponse response){
if (!StrUtil.isBlank(fileName)){
//设置文件路径
File file = new File(filePath+fileName);
if (file.exists()){
// 设置强制下载不打开
response.setContentType("application/force-download");
// 设置文件名
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
// return "下载成功!";
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
// return "下载失败!";
}
/**
* 文件上传工具类
* @param file
* @param filePath
* @return
*/
private String uploadFile(MultipartFile file, String filePath){
// 获取文件名
String fileName = file.getOriginalFilename();
// 获取日期,拼接到文件名中,避免文件名重复
String today = DateUtil.today();
File newFile = new File(filePath, today + fileName);
// 检测是否存在该目录
if (!newFile.getParentFile().exists()){
newFile.getParentFile().mkdirs();
}
try {
// 写入文件
file.transferTo(newFile);
return filePath+today+fileName;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}