springMVC文件上传
增加上传组件依赖:
In the case of the CommonsMultipartResolver
, you need to use commons-fileupload.jar
.
apache-commons-io.jar
apache-commons-fileupload.jar
- <!-- fileupload -->
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.4</version>
- </dependency>
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3</version>
- </dependency>
配置文件上传相关配置,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: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">
- <!-- 自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter -->
- <!-- 两个bean 是spring MVC为@Controllers分发请求所必须的 -->
- <mvc:annotation-driven/>
- <!-- 配置需要被扫描的包 -->
- <context:component-scan base-package="com.gc.springmvc.controller"/>
- <!-- 配置对静态资源文件的访问不被过滤 WebContent/resources目录中的文件都能访问 -->
- <mvc:resources location="/resources/" mapping="/resources/**"/>
- <!-- 配置返回的数据如何呈现:前缀+逻辑视图+后缀 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- <!-- 文件上传 -->
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!-- Default is ISO-8859-1 -->
- <property name="defaultEncoding" value="UTF-8"/>
- <!-- Default is 10240 -->
- <property name="maxInMemorySize" value="10240"/>
- <!-- -1 indicates no limit (the default) -->
- <property name="maxUploadSize" value="100000"/>
- <!-- javax.servlet.context.tempdir(the default) 临时文件存放目录,不是最终目录!-->
- <property name="uploadTempDir" value="/tempdir"/>
- </bean>
- </beans>
上传页面
- <form action="fileupload" method="post" enctype="multipart/form-data">
- 文件名称:
- <input type="text" name="name"/> <br/>
- 选择文件:
- <input type="file" name="file"/> <br/>
- <input type="submit"/>
- </form>
上传控制器
- package com.gc.springmvc.controller;
- import java.io.File;
- import java.util.Date;
- import javax.servlet.ServletContext;
- 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.context.ServletContextAware;
- import org.springframework.web.multipart.commons.CommonsMultipartFile;
- import org.springframework.web.servlet.view.InternalResourceViewResolver;
- @Controller
- public class FileUploadController implements ServletContextAware{
- private ServletContext servletContext;
- public void setServletContext(ServletContext servletContext) {
- this.servletContext = servletContext;
- }
- /**
- * 文件上传
- * @param assignedName 为文件指定新的名称
- * @param file 上传的文件
- * @return
- */
- @RequestMapping(value={"fileupload"}, method=RequestMethod.POST)
- public String fileupload(@RequestParam("name")String assignedName,@RequestParam("file")CommonsMultipartFile file) {
- String viewName = "uploadSuccess";
- if(!file.isEmpty()) {
- //通过servletContext获取到文件的绝对路径
- String realPath = servletContext.getRealPath("/upload");
- System.out.println("文件被保存到:"+realPath);
- System.out.println("指定的名称:"+assignedName);
- String originalFileName = file.getOriginalFilename();
- System.out.println("原始名称:"+originalFileName);
- String fileType = originalFileName.substring(originalFileName.lastIndexOf("."));
- System.out.println("文件类型:"+fileType);
- File targetFile = new File(realPath, assignedName+"-"+new Date().getTime()+fileType);
- try {
- file.getFileItem().write(targetFile);
- } catch (Exception e) {
- e.printStackTrace();
- viewName = "uploadFailure";
- };
- }
- //重定向
- return InternalResourceViewResolver.REDIRECT_URL_PREFIX+viewName;
- }
- /**
- * 返回成功视图
- */
- @RequestMapping(value="uploadSuccess")
- public String uploadSuccess() {
- return "uploadSuccess";
- }
- /**
- * 返回失败视图
- */
- @RequestMapping("uploadFailure")
- public String uploadFailure() {
- return "uploadFailure";
- }
- }