总结
文件上传
1) Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver实现的。
2) Spring 用 Jakarta Commons FileUpload 技术实现了一个 MultipartResolver 实现类:CommonsMultipartResolver
3) Spring MVC 上下文中默认没有装配 MultipartResovler,因此默认情况下不能处理文件的上传工作,如果想使用 Spring 的文件上传功能,需现在上下文中配置 MultipartResolver。
设置步骤:
1)导入jar包
commons-fileupload-1.2.1.jar
commons-io-2.0.jar
2)配置文件上传解析器,
<!-- 处理文件,将客户端上传的File文件,处理为MultipartFile 注意:文件解析器的bean中id必须设置为multipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置文件解析的编码,注意:一定要和页面的pageEncoding保持一致 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 设置最大上传文件大小, 注意不能写表达式,例如1024*1024,因为xml为纯文本存储,相当于都是字符串,不能处理表达式--> <property name="maxUploadSize" value="88888888"></property> </bean>
3)上传页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="down">下载图片</a> <form action="up1" method="post" enctype="multipart/form-data"> 头像:<input type="file" name="uploadFile" /> 描述:<input type="text" name="desc" /> <input type="submit" value="上传1" /> </form> <form action="up2" method="post" enctype="multipart/form-data"> 头像:<input type="file" name="uploadFile" /> 描述:<input type="text" name="desc" /> <input type="submit" value="上传2" /> </form> </body> </html>
4)控制器方法
package com.spring.controller; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; import javax.servlet.http.HttpSession; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; @Controller public class Download { @RequestMapping("/down") public ResponseEntity<byte[]> down(HttpSession session) throws IOException{ //获取下载文件的路径 String realPath = session.getServletContext().getRealPath("img"); String finalPath = realPath + File.separator + "2.jpg"; InputStream is = new FileInputStream(finalPath); //available():获取输入流所读取的文件的最大字节数 byte[] b = new byte[is.available()]; is.read(b); //设置请求头 HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment;filename=zzz.jpg"); //设置响应状态 HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(b, headers, statusCode); return entity; } @RequestMapping(value="/up1", method=RequestMethod.POST)
//注意参数名称和表单name一致 public String up_old(String desc, MultipartFile uploadFile, HttpSession session) throws IOException { //获取上传文件的名称 String fileName = uploadFile.getOriginalFilename();
//上传文件的保存路径 String path = session.getServletContext().getRealPath("photo") + File.separator + fileName; //获取输入流 InputStream is = uploadFile.getInputStream(); //获取输出流 File file = new File(path); OutputStream os = new FileOutputStream(file); /*int i = 0; while((i = is.read()) != -1) { os.write(i); }*/ int i = 0; byte[] b = new byte[1024]; while((i = is.read(b)) != -1) { os.write(b, 0, i); } os.close(); is.close(); return "success"; } @RequestMapping(value="/up2", method=RequestMethod.POST) public String up(String desc, MultipartFile uploadFile, HttpSession session) throws IOException { //获取上传文件的名称 String fileName = uploadFile.getOriginalFilename(); String finalFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf(".")); String path = session.getServletContext().getRealPath("photo") + File.separator + finalFileName; File file = new File(path); uploadFile.transferTo(file); return "success"; } }