Controller:
@Controller
public class UploadController {
@RequestMapping("upload")
public String upload(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
try {
response.setContentType("text/html;charset=UTF-8");
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
MultipartFile ffile = multipartRequest.getFile("file");
String path="";
if(ffile != null){
String ffileName = ffile.getOriginalFilename();
String tfileName = UUID.randomUUID().toString()+ffileName.substring(ffileName.lastIndexOf('.'), ffileName.length());
path=request.getSession().getServletContext().getRealPath("")+File.separator+"upload"+File.separator+tfileName;
File tfile = new File(path);
ffile.transferTo(tfile);
}
modelMap.put("result", "上传成功, "+path);
} catch (Exception e) {
e.printStackTrace();
modelMap.put("result", "上传失败");
}
return "fileupload";
}}
配置文件:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.mvc.controller" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<!-- 方法1 -->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" -->
<!-- p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> -->
<!-- 方法1 -->
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename">
<value>spring-views</value><!-- src下面spring-views.properties -->
</property>
</bean>
<!-- Spring的配置文件中添加-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>
upload.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传例子</title>
</head>
<body>
<div><font style="color:red;"><c:out value="${result}"/></font></div>
<form id="fileUploadForm" method="post" action="upload.do" enctype="multipart/form-data">
<tr>
<td>File:</td>
<td><input type="file" name="file"></td>
</tr>
<tr>
<td><input type="submit" value="submit" ></td>
</tr>
</form>
</body>
</html>
spring-views.properties:
fileupload.(class)=org.springframework.web.servlet.view.JstlView
fileupload.url=/upload.jsp