SpringBoot文件上传的几种常见方式(单文件上传、多文件上传)
作者:1加1等于
文件上传功能在很多系统中都占据非常重要的份置,软件开发人员在实现文件上传时,除了需要确保程序代码的稳健性和高维护性外,还需要从社会责任的角度审的需求,秉承合法、合规的职业理念进行数据读写,避免程序在保存用户上传的文件时造成不良的社会影响。例如,应该关注保护用户的隐私,避免用户的隐私泄露,以及杜绝上传非法文件。
文件上传的常见场景
在日常开发中,文件上传的场景多种多样。比如,在线教育平台上的视频资源上传,社交平台上的图片分享,以及企业内部的知识文档管理等。这些场景对文件上传的要求也各不相同,有的追求速度,有的注重稳定性,还有的需要考虑文件大小和安全性。因此,针对不同需求,我们有了秒传、断点续传和分片上传等解决方案。
文件上传是一个常见的需求。例如社交平台用户上传头像、电商系统商家上传商品图片、文档管理系统用户上传资料等都会用到。本文详细介绍SpringBoot关于单文件上传、多文件上传以及限制文件大小和类型等几种常见方式。
1. 单文件上传
单文件上传是最基础的文件上传场景,当用户选择一个文件并提交到服务器,可以使用 MultipartFile
类型来接收上传的文件。
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; @Controller public class SingleFileUploadController { @PostMapping("/singleFileUpload") @ResponseBody public String singleFileUpload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "请选择要上传的文件"; } try { // 获取文件名 String fileName = file.getOriginalFilename(); // 定义文件存储路径 String filePath = "uploads/"; File dest = new File(filePath + fileName); // 检查目录是否存在,不存在则创建 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } // 保存文件 file.transferTo(dest); return "文件上传成功"; } catch (IOException e) { e.printStackTrace(); return "文件上传失败"; } } }
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>单文件上传</title> </head> <body> <form action="/singleFileUpload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传"> </form> </body> </html>
enctype="multipart/form-data"
是必须的
2. 多文件上传
多文件上传允许用户一次选择多个文件进行上传。可以使用 MultipartFile
数组来接收多个上传的文件。
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; @Controller public class MultipleFileUploadController { @PostMapping("/multipleFileUpload") @ResponseBody public String multipleFileUpload(@RequestParam("files") MultipartFile[] files) { if (files.length == 0) { return "请选择要上传的文件"; } for (MultipartFile file : files) { if (file.isEmpty()) { continue; } try { String fileName = file.getOriginalFilename(); String filePath = "uploads/"; File dest = new File(filePath + fileName); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } file.transferTo(dest); } catch (IOException e) { e.printStackTrace(); return "文件上传失败"; } } return "文件上传成功"; } }
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>多文件上传</title> </head> <body> <form action="/multipleFileUpload" method="post" enctype="multipart/form-data"> <input type="file" name="files" multiple> <input type="submit" value="上传"> </form> </body> </html>
input组件需要 multiple
支持多文件选择
3. 限制文件大小和类型
在实际开发的时候,可能需要对上传的文件大小和类型进行限制。可以通过配置SpringBoot的 MultipartConfigElement
来实现文件大小限制,同时在代码中检查文件类型。
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; @Configuration public class MultipartConfig { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // 单个文件最大10MB factory.setMaxFileSize(DataSize.ofMegabytes(10)); // 设置总上传数据总大小20MB factory.setMaxRequestSize(DataSize.ofMegabytes(20)); return factory.createMultipartConfig(); } }
检查文件类型:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; @Controller public class FileUploadWithLimitController { @PostMapping("/fileUploadWithLimit") @ResponseBody public String fileUploadWithLimit(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "请选择要上传的文件"; } // 检查文件类型 String contentType = file.getContentType(); if (!"image/jpeg".equals(contentType) && !"image/png".equals(contentType)) { return "只允许上传jpeg和png格式文件"; } try { String fileName = file.getOriginalFilename(); String filePath = "uploads/"; File dest = new File(filePath + fileName); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } file.transferTo(dest); return "文件上传成功"; } catch (IOException e) { e.printStackTrace(); return "文件上传失败"; } } }
getContentType
方法可以获取文件的MIME类型
到此这篇关于SpringBoot实战——文件上传一篇搞定的文章就介绍到这了,更多相关SpringBoot文件上传内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!