一. 配置pom.xml
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3</version> </dependency>
二. SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置springmvc中的MultipartResolver:用于处理表单中的file
<!-- 配置文件上传,如果没有使用文件上传可以不用配置 --> <bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="1024" /> </bean>
三.编写jsp页面(文件上传和文件下载页面)
<%@ 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> <form action="springmvc/uploads" method="post" enctype="multipart/form-data"> 文件1:<input type="file" name="files"> <br> 文件2:<input type="file" name="files"> <br> 文件3:<input type="file" name="files"> <br> <input type="submit" value="多文件上传"> </form> <form action="springmvc/upload" method="post" enctype="multipart/form-data"> 文件:<input type="file" name="file"> <br> <input type="submit" value="单文件上传"> </form> <a href="springmvc/download">下载a.txt文件</a> </body> </html>
注意要在form标签中加上enctype="multipart/form-data"表示该表单是要处理文件的,这是最基本的东西,很多人会忘记然而当上传出错后则去找程序的错误,却忘了这一点。
四.编写控制类
1.多文件上传控制类
//文件上传 private void saveFile(MultipartFile file) { if (!file.isEmpty()) { String rootPath = "C:\\Programme Software\\eclipse\\eclipse\\eclipse-workspace\\upload\\"; String fileName = file.getOriginalFilename(); File writeFile = new File(rootPath, fileName); try { file.transferTo(writeFile); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } //文件上传 @RequestMapping(value = "/uploads") public String Upload(MultipartFile[] files) throws Exception { for (MultipartFile file : files) { saveFile(file); } return "success"; }
通过MultipartFile的transferTo(File dest)这个方法来转存文件到指定的路径。
2.单文件上传控制类
//单文件上传 @RequestMapping(value = "/upload") public String Upload(MultipartFile file) throws Exception { if (!file.isEmpty()) { String rootPath = "C:\\Programme Software\\eclipse\\eclipse\\eclipse-workspace\\upload\\"; String fileName = file.getOriginalFilename(); File writeFile = new File(rootPath, fileName); try { file.transferTo(writeFile); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return "success"; }
3.文件下载控制类
//文件下载 @RequestMapping("/download") public ResponseEntity<byte[]> DownloadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 接受的是UTF-8 req.setCharacterEncoding("utf-8"); // 获取项目根目录 String path = "C:\\Programme Software\\eclipse\\eclipse\\eclipse-workspace\\upload\\a.txt"; // 获取文件名 String filename = "a.txt"; File file = null; HttpHeaders headers = null; try { System.out.println(filename);// myfiles file = new File(path); // 请求头 headers = new HttpHeaders(); String fileName1 = new String(filename.getBytes("UTF-8"), "iso-8859-1");// 解决文件名乱码 // 通知浏览器以attachment(下载方式)打开图片 headers.setContentDispositionFormData("attachment", fileName1); // application/octet-stream二进制流数据(最常见的文件下载)。 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); } catch (Exception e) { throw new RuntimeException(e); } return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); }