springmvc文件上传
1、maven依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ly.mvc</groupId> <artifactId>springmvc03</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc03 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>13</maven.compiler.source> <maven.compiler.target>13</maven.compiler.target> <!--锁定spring版本--> <spring.version>5.0.2.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!--文件上传,依赖commons-io--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> </dependencies> <build> <finalName>springmvc03</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <!--解决IDEA maven变更后自动重置LanguageLevel和JavaCompiler版本的问题--> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
2、上传页面
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2020/2/28 Time: 16:00 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--文件上传时method必须是post、enctype必须是multipart/form-data--%> <form action="upload" method="post" enctype="multipart/form-data"> <input type="file" name="upload"/><br> <input type="submit" value="提交"/> </form> </body> </html>
3、springmvc.xml
<?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: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.ly.mvc"/> <!--配置视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--配置文件上传解析器,id必须时multipartResolver--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--最大上传大小,单位字节,10MB是10*1024*1024=--> <property name="maxUploadSize" value="10485760"></property> </bean> <!--开启springmvc注解驱动--> <mvc:annotation-driven/> </beans>
4、处理方法
/** *<input type="file" name="upload"/><br> *springmvc文件上传,表单上传文件的name属性必须与处理方法的MultipartFile类型的参数名一致 */ @RequestMapping("/upload") public String upload(HttpServletRequest request,MultipartFile upload) throws IOException { //美女.jpg String filename = upload.getOriginalFilename(); //UUID将-替换成""再拼接上文件后缀 filename = UUID.randomUUID().toString().replace("-","") + "."+filename.split("\\.")[1]; //文件上传路径 String path = request.getSession().getServletContext().getRealPath("/uploads/"); //该路径是否存在,若不存在则创建 File file = new File(path); if(!file.exists()) { file.mkdirs(); } //上传文件 upload.transferTo(new File(path,filename)); return "success"; }
5、总结
5.1、上传页面表单method必须为post、enctype必须是multipart/form-data
5.2、springmvc.xml中配置的文件解析器id必须为multipartResolver
5.3、上传页面中表单上传文件的name属性必须与处理方法中的MultipartFile类型的参数名相同
相识是缘