目录结构如下:
注意,下面说的配置文件,一般都是值的src下的配置文件,即mvc.xml。如果是web.xml,则直接说 web.xml
1. 文件上传的注意点
表单必须是post提交,必须将 enctype 设置为 “multipart/form-data”,
使用 commons-fileupload 提交文件,需要添加 commons-fileupload 和 commons-io 的 jar 包。
2.Jsp 页面
<form action="file/upload.do" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"/><input type="submit" value="上传"/>
</form>
</body>
3.Controller类
@Controller
//窄化
@RequestMapping("/file")
public class UploadController {
@RequestMapping("/upload.do")
public String upload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest req) throws Exception{
String path=req.getServletContext().getRealPath("/upload");
//获取文件名
String fileName=file.getOriginalFilename();
InputStream is = file.getInputStream();
OutputStream os = new FileOutputStream(new File(path,fileName));
byte[] buffer = new byte[400];
int len=0;
while((len=is.read(buffer))!=-1){
os.write(buffer, 0, len);
}
os.close();
is.close();
return "redirect:/index.jsp";
}
}
4. 在配置 文件中添加 multipartResolver
<!-- 文件上传配置 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000"/>
</bean>
附录:
附一,这里附上mvc.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"
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">
<!-- 注解开发适配器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<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">
<property name="maxUploadSize" value="1000000"/>
</bean>
<!-- 扫描注解类 -->
<context:component-scan base-package="cn.sxt.controller"/>
</beans>
这里再附上 WebContent/WEB-INF/ 下的 web.xml 文件内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>01springmvc_helloworld</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 改变springmvc配置文件的路径及名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>