如下示例:
页面:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>etoak</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>etoak</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-a
etoakk-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--
请求解析器
字符请求解析器 HandlerMapping
字节请求解析器 MultipartResolver
CommonsMultipartResolver
当服务器端使用commons-fileupload处理上传请求时,使用该解析器
StandardServletMultipartResolver
当服务器段使用smartupload处理上传请求时,使用该解析器
注意:在注册上传请求解析器时,该解析器的名字(id)值必须是 : multipartResolver
接口名-首字母小写
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
<context:component-scan base-package="com"/>
</beans>
FileController.java:
@Controller
public class FileController {
@RequestMapping("/upload")
public String upload(User user ,
HttpServletRequest request)throws Exception{
MultipartFile myfile = user.getMyfile();
String filename = myfile.getOriginalFilename();
String contentType = myfile.getContentType();
long size = myfile.getSize();
// 从上传文件中获取一个输入流
InputStream is = myfile.getInputStream();
// 定位到file目录 request.session.ServletContext.getRealPath("/file")
String path = request.getSession().getServletContext().getRealPath("/file");
String newFilename = new UUIDGenerator().generate().toString()+
filename.substring(filename.lastIndexOf("."));
File file = new File(path+"/"+newFilename);
OutputStream os = new FileOutputStream(file);
int len;
byte[] data = new byte[1024];
while((len=is.read(data))!=-1)
os.write(data, 0, len);
is.close();
os.close();
return "redirect:success.jsp";
}
@RequestMapping("/download")
public void download(String filename,HttpServletRequest request,HttpServletResponse response) throws Exception{
System.out.println("文件名"+filename);
String path = request.getSession().getServletContext().getRealPath("/file");
File file = new File(path+"/"+filename);
response.setContentType("multipart/form-data");
response.setHeader("content-Disposition", "attachment;filename="+filename);
InputStream is = new FileInputStream(file);
OutputStream os = response.getOutputStream();
int len;
byte[] data = new byte[1024];
while((len=is.read(data))!=-1)
os.write(data, 0, len);
is.close();
os.close();
}
}
User.java:
package com.etoak.bean;
import org.springframework.web.multipart.MultipartFile;
public class User {
/** myfile 文件
* spring-mvc如何封装文件类型对象
*
* struts1 - FormFile
* struts2 - File String String
* spring-mvc - MultipartFile接口 、 CommonsMultipartFile
*/
private MultipartFile myfile;
public MultipartFile getMyfile() {
return myfile;
}
public void setMyfile(MultipartFile myfile) {
this.myfile = myfile;
}
}