spring-mvc.xml文件下配置:
<!-- 自动扫描包 -->
<context:component-scan base-package="com.wd">
</context:component-scan>
<!--配置视图解析器 定义JSP文件的位置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--文件上传配置 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000" /><!-- 10M -->
</bean>
web.xml:
<!--声明一个前端控制器 DispatcherServlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--表示启动容器时初始化该Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- spring Web MVC框架提供了org.springframework.web.filter.CharacterEncodingFilter
用于解决POST方式造成的中文乱码问题 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
**controller层代码如下:**
package com.wd.controllers;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javassist.bytecode.Descriptor.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller
public class UpLoadFile {
@RequestMapping(value = "upload2", method = RequestMethod.POST)
public String upLoad2(HttpServletRequest request,
HttpServletResponse response) throws IllegalStateException,
IOException {
// 解析器解析request的上下文
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 先判断request中是否包涵multipart类型的数据,
if (multipartResolver.isMultipart(request)) {
// 再将request中的数据转化成multipart类型的数据
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
java.util.Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile((String) iter.next());
if (file != null) {
String fileName = file.getOriginalFilename();
System.out.println(fileName);
String path = "E:/MyeclipseWorkSpace/springmvc/WebRoot/file/" + fileName;
File localFile = new File(path);
// 写文件到本地
file.transferTo(localFile);
}
}
}
return "result";
}
}
WebRoot根目录下index.jsp页面代码:
<form action="upload2" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
</form>
WebRoot根目录下 result.jsp 代码如下
<body>
<body>
<h1>上传成功!</h1>
</body>
</body>