文件上传
SpringMVC中,为文件的上传提供了直接的支持,这种支持是使用可拔插的组件MultipartResolver实现的,Spring使用FileUpload实现了MultipartResolver接口,实现类名称为CommonsMultipartResolver,SpringMVC上下文中并没有默认装配MultipartResolver,因此默认情况是进行不了文件上传的,需要我们在SpringMVC上下文中配置MultipartResolver这个bean。
范例
1. 添加jar包
commons-fileupload-1.2.1.jar
commons-io-2.0.jar
2. 更改配置文件
<!-- 配置MultipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- 文件上传的最大字节数 1024*1024*5 -->
<property name="maxUploadSize" value="#{1024*1024*5}"></property>
//<property name="maxUploadSize" value="6000000"></property>
</bean>
3. 编写文件上传的表单
注意:
①文件上传的表单得是 post 请求
②修改enctype=“multipart/form-data”,不使用默认值。
<form action="${pageContext.request.contextPath}/testFileUpload" method="post" enctype="multipart/form-data">
File:<input type="file" name="file1">
<br>
File:<input type="file" name="file2">
<br>
Desc:<input type="text" name="desc">
<br>
<button>Test FileUpload</button>
</form>
4. 编写请求处理器
@RequestMapping("testFileUpload")
public String testFileUpload(String desc,MultipartFile file1,MultipartFile file2,HttpSession session) throws Exception {
// System.out.println("=============" + desc);
// System.out.println("文件名称:"+file1.getName());
// System.out.println("文件扩展名:" + file1.getOriginalFilename());
// System.out.println("文件大小:" + file1.getSize());
File newfile1 = new File(session.getServletContext().getRealPath("/file")+File.separator+file1.getOriginalFilename());
File newfile2 = new File(session.getServletContext().getRealPath("/file")+File.separator+file2.getOriginalFilename());
file1.transferTo(newfile1);//将上传文件另存为一个新的文件
file2.transferTo(newfile2);//将上传文件另存为一个新的文件
return "success";
}
文件下载
参考上一篇文章SpringMVC—HttpMessageConverter原理
实现文件的上传和下载
1. 除基本的jar包外,添加的jar包
commons-fileupload-1.2.1.jar
commons-io-2.0.jar
2. 更改配置文件
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- 文件上传的最大字节数 1024*1024*5 -->
<property name="maxUploadSize" value="#{1024*1024*5}"></property>
//<property name="maxUploadSize" value="6000000"></property>
</bean>
3. 编写success.jsp
<form action="${pageContext.request.contextPath }/testFileUpload" enctype="multipart/form-data" method="post">
名称:<input type="text" name="picname" /><br />
图片:<input type="file" name="file1"><br />
<input type="submit" value="上传" />
</form>
<br />
<a href="${pageContext.request.contextPath }/testFileDownLoad">文件下载</a>
4. 编写请求处理器
/**
* 文件的上传
* @param picname
* @param file1
* @param session
* @return
* @throws Exception
*/
@RequestMapping("/testFileUpload")
public String testFileUpload(@RequestParam(value="picname",required=false) String picname,MultipartFile file1,HttpSession session) throws Exception {
//获取file文件实际路径
String path = session.getServletContext().getRealPath("/file");
File file = new File(path);
//如果file不存在,则创建一个file文件夹
if(!file.exists()) {
file.mkdirs();
}
//获取上传文件的名称
String filename = file1.getOriginalFilename();
file1.transferTo(new File(file, filename));//将上传文件另存为一个新的文件
//转发到success.jsp
return "success";
}
/**
*文件下载
* @param req
* @param resp
* @return
* @throws Exception
*/
@ResponseBody
@RequestMapping("//testFileDownLoad")
public byte[] testFileDownLoad(HttpServletRequest req,HttpServletResponse resp) throws Exception{
//获取file文件实际路径
String path = req.getSession().getServletContext().getRealPath("/file");
//其中404.jpg可以从前端传过来的其他名称
File file = new File(path, "404.jpg");
//输入流
InputStream in = new FileInputStream(file);
byte b[] = new byte[in.available()];
in.read(b);
//生成一个唯一的文件名称
String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
resp.setHeader("Content-Disposition", "attachment;filename="+ uuid +".jpg");
in.close();
return b;
}