下面就是SpringMVC文件上传配置的完整攻略。
SpringMVC 文件上传配置
1. 添加依赖
在pom.xml
中添加以下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.6</version>
</dependency>
2. 配置 web.xml 文件
在 web.xml 中添加 SpringMVC 的配置:
<!-- SpringMVC 配置 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springDispatcherServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3. 配置 Spring 的配置文件
在 /WEB-INF/springDispatcherServlet-servlet.xml
中配置 Spring 的配置文件:
<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">
<!-- 启用 SpringMVC -->
<mvc:annotation-driven />
<!-- 配置上传文件大小限制 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880"/>
</bean>
<!-- Controller 配置 -->
<context:component-scan base-package="com.yourpackage.controller" />
<!-- 视图解析器配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
4. 编写 Controller
编写文件上传的 Controller:
@Controller
public class UploadController {
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@RequestParam("file") MultipartFile file, Model model) {
String fileName = file.getOriginalFilename();
try {
file.transferTo(new File("D:\\uploads\\" + fileName));
model.addAttribute("message", "文件上传成功");
} catch (Exception e) {
model.addAttribute("message", "文件上传失败");
}
return "uploads";
}
}
5. 编写 View
编写上传文件的 View:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>SpringMVC 文件上传示例</title>
</head>
<body>
<h3>上传文件</h3>
<form method="post" action="${pageContext.request.contextPath}/upload" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
<hr/>
<!-- 文件上传反馈信息 -->
${message}
</body>
</html>
6. 多文件上传示例
如果需要上传多个文件,只需要将 @RequestParam
的类型修改为 MultipartFile[]
即可:
@Controller
public class UploadController {
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@RequestParam("file") MultipartFile[] files, Model model) {
try {
for (MultipartFile file : files) {
String fileName = file.getOriginalFilename();
file.transferTo(new File("D:\\uploads\\" + fileName));
}
model.addAttribute("message", "文件上传成功");
} catch (Exception e) {
model.addAttribute("message", "文件上传失败");
}
return "uploads";
}
}
同时,在 View 中可以使用数组形式的输入框:
<form method="post" action="${pageContext.request.contextPath}/upload" enctype="multipart/form-data">
<input type="file" name="file"/><br/>
<input type="file" name="file"/><br/>
<input type="file" name="file"/><br/>
<input type="submit" value="上传"/>
</form>
这样就可以同时上传多个文件了。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringMVC 文件上传配置,多文件上传,使用的MultipartFile的实例 - Python技术站