SpringMVC文件上传

xiaoxiao2021-02-28  109

1、搭建Web项目

(1)加入文件上传所需依赖

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springmvc</groupId> <artifactId>springmvc-demo</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>springmvc-demo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!--使用单元测试所需jar包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--spring相关包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--servlet-api--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!--fileupload--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> </dependencies> <build> <finalName>springmvc-demo</finalName> </build> </project> (2)web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--配置要加载的XML文件--> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> (3)springmvc-servlet.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.springmvc.*"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!--文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--编码--> <property name="defaultEncoding" value="UTF-8"></property> </bean> </beans>(4)项目结构

2、创建IndexController,创建fileupload方法来接收上传文件的请求,MultipartFile用来接收文件

@Controller public class IndexController { @RequestMapping("/index") public String index(){ return "index"; } @RequestMapping("/fileupload") public String fileupload(@RequestParam("file") MultipartFile multipartFile , HttpServletRequest request,RedirectAttributes attr){ // String filepath=request.getSession().getServletContext().getRealPath("/");//获取项目绝对路径 String filepath="/Users/shanmenglu/IdeaProjects/springmvc-demo/upload";//文件存储路径 File file=new File(filepath,multipartFile.getOriginalFilename()); try { multipartFile.transferTo(file); attr.addFlashAttribute("success","上传成功!");//使用RedirectAttributes在重定向时可以传递参数 } catch (IOException e) { e.printStackTrace(); } return "redirect:index"; } }

3、index.jsp

<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> <html> <body> <h2>fileupload</h2> <form method="post" action="/fileupload" enctype="multipart/form-data"> 上传图片: <input type="file" name="file"> <input type="submit" value="submit"> </form> ${success} </body> </html>

4.测试

上传文件

到项目下upload目录查看

代码下载:http://download.csdn.net/detail/lom9357bye/9864067

转载请注明原文地址: https://www.6miu.com/read-25209.html

最新回复(0)