[刘阳Java]_SpringMVC文件上传第2季_第11讲
这篇文章重点介绍基于SpringMVC的多文件上传,代码实现难度不大。只是我们相对于SpringMVC文件上传第1季中加入异常处理的判断
1. 实现案例的截图
2. 文件上传功能需求
- 可以选择多个文件上传
- 如果没有选择文件就上传需要异常处理
- 如果服务器上已经有相同的文件,需要异常处理
3. 功能实现步骤
- 添加支持文件上传的解析器
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.springmvc.controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5242880"></property> <property name="maxInMemorySize" value="4096"></property> <property name="defaultEncoding" value="UTF-8"></property> </bean> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.Throwable">error</prop> </props> </property> </bean> </beans>
- 编写文件上传的控制器代码
package com.springmvc.controller; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @Controller @RequestMapping("/upload") public class FileUploadController { @RequestMapping(value="/multifileupload", method=RequestMethod.POST) public void upload(@RequestParam(value="myfiles") MultipartFile[] myfiles, HttpServletRequest request, HttpServletResponse response) throws IOException { long start = System.currentTimeMillis(); String savePath = request.getServletContext().getRealPath("/") + "upload"; savePath = savePath.replaceAll("\\\\", "/"); File dirs = new File(savePath); //保存路径 if (!dirs.exists()) { dirs.mkdirs(); } response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); for (MultipartFile myfile : myfiles) { //遍历多个上传文件 String originalFileName = myfile.getOriginalFilename(); //上传文件名称 long fileSize = myfile.getSize(); //上传文件大小 String fileContentType = myfile.getContentType(); //上传文件类型 myfile.transferTo(new File(dirs, originalFileName)); //开始上传文件 long end = System.currentTimeMillis(); out.println("上传文件的名称 = " + originalFileName); out.println("<p>"); out.println("保存文件的路径 = " + savePath); out.println("<p>"); out.println("上传文件的大小 = " + fileSize); out.println("<p>"); out.println("上传文件的类型 = " + fileContentType); out.println("<p>"); out.println("上传成功的时间 = " + String.valueOf(end - start) + "ms"); out.println("<p>"); out.flush(); } out.close(); } }
- 编写文件上传的html代码,注意表单中需要加入enctype="multipart/form-data"
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="upload/multifileupload.do" method="post" enctype="multipart/form-data"> <fieldset> <legend>多个文件上传</legend> <input type="file" name="myfiles"> <input type="file" name="myfiles"> <input type="submit"> </fieldset> </form> </body> </html>
源码下载地址:https://pan.baidu.com/s/1eSDZwFg
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下