SpringBoot文件上传和下载
一、SpringBoot文件上传
1、SpringMVC文件上传
1、Client处理 选择文件 (1)form表单 选择文件 method="post" (2)enctype="multipart/form-data" (3) 准备文件框<input type="file" name="upload"> 2、服务端准备存储上传文件的目录 新建文件夹 项目/webapp/upload文件 3、开发文件上传Controller public String fileUpload(MultipartFile upload){ //接收数据 //调用业务 接收的文件--》服务文件目录中 文件拷贝 upload.transforTo(new File(父目录,当前文件名)) //跳转页面 return "ok" //ok.jsp } 4、配置文件上传解析器CommonsMultipartResolver id必须为MulyipartResolver 5、commons-fileUpload[引入] Commons-io
2、SpringBoot文件上传
- 文件上传所需依赖
<!--集成springboot的父项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <dependencies> <!--引入springboot的web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入jsp解析相关依赖--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> </dependencies>
Application.yml文件配置
server: port: 8080 servlet: jsp: init-parameters.development: true #开启jsp页面的热部署 spring: mvc: view: prefix: / suffix: .jsp
(1)单个文件上传案例
- 前端
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> </head> <body> <form action="${pageContext.request.contextPath}/file/upload" method="post" enctype="multipart/form-data"> <input type="file" name="upload"> <input type="submit" value="上传文件"> </form> </body> </html>
控制层
@Controller @RequestMapping("file") public class upload { @RequestMapping("upload") public String upload(MultipartFile upload, HttpServletRequest request) throws IOException { String realPath = request.getSession().getServletContext().getRealPath("upload"); //调用业务 文件拷贝 //处理文件名 UUID拼串=uuid+"."+扩展名 String s = UUID.randomUUID().toString().replace("-", "") + "." + FilenameUtils.getExtension(upload.getOriginalFilename()); //调用业务 文件拷贝 upload.transferTo(new File(realPath,s)); return "index"; } }
(2)多个文件上传案例
- 前端
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> </head> <body> <form action="${pageContext.request.contextPath}/file/upload" method="post" enctype="multipart/form-data"> <input type="file" name="upload"></br> <input type="file" name="upload"></br> <input type="file" name="upload"> <input type="submit" value="上传文件"> </form> </body> </html>
控制层
/** * @author 王恒杰 * @date 2021/11/26 10:24 * @Description: */ @Controller @RequestMapping("file") public class upload { @RequestMapping("upload") public String upload(MultipartFile[] upload, HttpServletRequest request) throws IOException { for (int i = 0; i < upload.length; i++) { //考虑有用户没有上传文件 if(upload[i]!=null){ String realPath = request.getSession().getServletContext().getRealPath("upload"); //处理文件名 UUID拼串=uuid+"."+扩展名 对文件名定义唯一标识 String s = UUID.randomUUID().toString().replace("-", "") + "." + FilenameUtils.getExtension(upload[i].getOriginalFilename()); //调用业务 文件拷贝 upload[i].transferTo(new File(realPath,s)); } } return "index"; } }
(3)文件上传大小设置
- 上传三个视频大小大概有100M
报错,显示文件太大
SpringBoot内置文件大小是10M
我们应该在application.yml
中对文件大小进行设置
#修改文件上传大小的限制 servlet: multipart: max-file-size: 209715200 #用来控制文件的上传 max-request-size: 209715200 #用来指定服务端最大文件大小
application.properties
#修改文件上传的大小限制 #设置上传的文件大小 spring.servlet.multipart.max-file-size=100MB #设置请求携带数据的大小 spring.servlet.multipart.max-request-size=100MB
二、文件下载
1、SpringMvc中文件下载
(1)在服务端文件系统中存储可以供下载的文件项目/webapp/download
aa.txt bb.txt
(2)在Client提供可以下载链接
<a href="/项目名/file/download?fileName=aa.txt">下载 aa.txt</a> • 1
(3)开发文件下载Controller
public String downLoad(String fileName,HttpRequest request,HttpResponse response){ //接收数据 要下载的文件名 //调用业务 服务器文件---》Client a、根据文件名去服务器上加载对应文件 输入流 InputStream is=new FileInputStream(new File(request.getSession().getServletVContext().getRealPath("/download"),fileName)); b、获取响应输出流 PrintWriter out= response.getWriter(); c、文件下载的方式 inline:浏览器尝试打开,默认形式 attachment:附件形式下载 response.setHeader("content-disposition","attachment;filename="+ERLEncoder.encode(fileName,"UTF-8")) d、拷贝文件 IOutil.copy(加载文件的输入流,响应输出流) e、关闭资源 IoUtil. //跳转页面 return null; }
2、SpringBoot文件下载
- 文件上传所需依赖
<!--集成springboot的父项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <dependencies> <!--引入springboot的web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入jsp解析相关依赖--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> </dependencies>
Application.yml文件配置
server: port: 8080 servlet: jsp: init-parameters.development: true #开启jsp页面的热部署 spring: mvc: view: prefix: / suffix: .jsp
前端页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <body> <h1>文件上传</h1> <a href="${pageContext.request.contextPath}/file/download?fileName=a.txt">下载 a.txt</a> <a href="${pageContext.request.contextPath}/file/download?fileName=b.txt">下载 b.txt</a> </body> </html>
控制层
@Controller @RequestMapping("file") public class upload { @RequestMapping("download") public String download(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException { //接收数据 文件名 //文件下载 //1、加载服务器指定名 String realPath = request.getSession().getServletContext().getRealPath("/download"); FileInputStream is = new FileInputStream(new File(realPath, fileName)); //2、设置文件下载方式 response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8")); //3、获取响应输出流 PrintWriter writer = response.getWriter(); //4、流数据交换 IOUtils.copy(is,writer); //5、关闭资源 IOUtils.closeQuietly(is); IOUtils.closeQuietly(writer); return null; }