标签:
一、在pom.xml中添加依赖
<!-- io包 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commonsio.version}</version>
</dependency>
<!-- 加入fileupload依赖包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
二、spring-mvc.xml配置文件上传
<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="com.leech.controller" /> <mvc:annotation-driven/> <!-- 配置静态资源不经过spring mvc --> <mvc:resources location="/static/" mapping="/static/**"/> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 文件上传配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean>
三、编写UploadController上传控制器
import org.apache.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller
@RequestMapping("/upload")
public class UploadController {
/**
* Logger for this class
*/
private static final Logger LOG = Logger.getLogger(UploadController.class);
@RequestMapping("/req")
public String req() {
return "upload";
}
@RequestMapping("/uploadHandler")
public String uploadHandler(@RequestParam(value = "file") CommonsMultipartFile file)throws IllegalStateException, IOException {
long startTime = System.currentTimeMillis();
LOG.debug("fileName:" + file.getOriginalFilename());
String path = "E:/temp/" + new Date().getTime()+ file.getOriginalFilename();
File newFile = new File(path);
// 通过CommonsMultipartFile的方法直接写文件(注意这个时候)
file.transferTo(newFile);
long endTime = System.currentTimeMillis();
LOG.debug("方法二的运行时间:" + String.valueOf(endTime - startTime)+ "ms");
return "success";
}
}
四、编写jsp页面
1、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="upload/req">springmvc-upload文件上传测试</a> </body> </html>
2、upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="uploadHandler" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="上传" /> </form> </body> </html>
3、success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 上传文件成功!<br/> <a href="req">继续上传</a> </body> </html>
标签:
原文地址:http://my.oschina.net/u/1757031/blog/509914