# 文件上传校验
# 开启上传和下载
spring.servlet.multipart.enabled=true
# 最大的文件大小
spring.servlet.multipart.max-file-size=50KB
# 单次最大请求大小
spring.servlet.multipart.max-request-size=50KB
@RestController
public class FileUploadController {
// 用于根据时间区分目录
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
@PostMapping("/upload")
public String upload(MultipartFile uploadFile, HttpServletRequest req){
// 上传文件路径
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
String format = sdf.format(new Date());
File folder = new File(realPath + format);
// 判断文件夹是否存在
if(!folder.isDirectory()){
folder.mkdirs();
}
// 获取原始名字
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
try{
uploadFile.transferTo(new File(folder, newName));
String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
return filePath;
}catch (IOException e){
e.printStackTrace();
}
return "上传失败";
}
// 批量即在单个的基础上增加遍历
@PostMapping("/uploads")
public String uploads(MultipartFile[] uploadFiles, HttpServletRequest req){
ArrayList<String> pathList = new ArrayList<>();
for(MultipartFile uploadFile : uploadFiles){
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
String format = sdf.format(new Date());
File folder = new File(realPath + format);
if(!folder.isDirectory()){
folder.mkdirs();
}
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
try{
// 将上传文件保存到一个目标文件中
uploadFile.transferTo(new File(folder, newName));
String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
pathList.add(filePath);
}catch (IOException e){
e.printStackTrace();
}
}
return pathList.toString();
//return "上传失败";
}
}
bytell getBytes()。获取文件数据
String getContentType()。获取文件 MIME 类型,如 image/ipeg 等.
InputStream getlnputStream()。获取文件流
String getName()。获取表单中文件组件的名字。
String getOriginalFilename()。获取上传文件的原名
long getSize()。获取文件的字节大小,单位为 byte。
boolean isEmpty()。是否有上传的文件
void transferTo(File dest)。将上传文件保存到一个目标文件中。
前台
单个上传
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" value="请选择文件"/>
<input type="submit" value="上传">
</form>
多个上传
<form action="/uploads" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFiles" value="请选择文件" multiple/>
<input type="submit" value="上传">
</form>
@GetMapping("/download")
public ResponseEntity<InputStreamResource> downloadFile(HttpServletRequest req, @RequestParam("filename") String filename) throws IOException{
// 下载文件路径
String path = req.getSession().getServletContext().getRealPath("/uploadFile/");
// File.separator /
InputStream is = new FileInputStream(path + File.separator + filename);
InputStreamResource resource = new InputStreamResource(is);
HttpHeaders headers = new HttpHeaders();
// 通知浏览器以attachment(下载方式)打开图片
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);
}