文章来源:https://blog.csdn.net/TomCat_And_Jerry/article/details/114142098
1 创建新项目
2 创建html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>up-down-files</title>
</head>
<body>
<h1>文件上传</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
<h1>文件下载</h1>
<a href="/download?fileName=pic1.png">pic1.png</a>
</body>
</html>
3 添加限制
# 指定服务器端文件大小限制
spring.servlet.multipart.max-file-size=300MB
# 指定客户端文件大小限制
spring.servlet.multipart.max-request-size=300MB
fileLocation=static/files
4 创建Controller
package com.example.up_down_files;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
@Controller
public class FileUploadController {
@Value("${fileLocation}")
private String fileLocation;
@PostMapping("upload")
@ResponseBody
private void upload(MultipartFile file) throws IOException {
System.out.println(file.getOriginalFilename());//fileName
System.out.println(file.getContentType());//fileType
System.out.println(file.getSize());//fileSize
System.out.println(file.getInputStream());//inputStream
// 获取web应用中files文件夹的绝对路径
String realPath = ResourceUtils.getURL("classpath:").getPath()+fileLocation;
System.out.println(fileLocation);
System.out.println(realPath);
File newFile = new File(realPath);
if(!newFile.exists()){
newFile.mkdirs();
}
Date date = new Date();
String filename = date.getTime()+"@"+file.getOriginalFilename();
// 上传
file.transferTo(new File(newFile,filename));
}
@GetMapping("download")
public void download(String fileName, HttpServletResponse response) throws IOException {
// 获取待下载文件所在文件夹的绝对路径
String realPath = ResourceUtils.getURL("classpath:").getPath()+fileLocation;
System.out.println(fileLocation);
// 获得文件输入流
FileInputStream inputStream = new FileInputStream(new File(realPath,fileName));
// 设置响应头、以附件形式打开文件
response.setHeader("content-disposition","attachment;fileName="+fileName);
ServletOutputStream outputStream = response.getOutputStream();
int len = 0;
byte[] data = new byte[1024];
while((len = inputStream.read(data))!=-1){
outputStream.write(data,0,len);
}
outputStream.close();
inputStream.close();
}
}
5 运行类
package com.example.up_down_files;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UpDownFilesApplication {
public static void main(String[] args) {
SpringApplication.run(UpDownFilesApplication.class, args);
}
}
6 运行结果
输出结果:
icon2.png
image/png
231891
java.io.FileInputStream@df7775c
static/files
/D:/JavaIdeaProjects/aispringboot-1/up_down_files/target/classes/static/files