單檔案上傳/下載
阿新 • • 發佈:2019-01-01
1.新增依賴
<!-- 檔案上傳 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2.jsp頁面開發
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); %> <!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>上傳檔案</title> </head> <body> <form action="<%=basePath%>/upload" method="post" enctype="multipart/form-data"> 上傳檔案:<input type="file" name="uploadFile"> <br> <input type="submit" value="上傳"> <br> 下載檔案:<a href="<%=basePath%>/download">下載</a> </form> </body> </html>
3.開發檔案處理類
@Controller public class FileController { //顯示檔案上傳頁面 @RequestMapping(value = "/display") public ModelAndView display(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("items/upload"); return modelAndView; } //上傳處理(web版) @RequestMapping(value = "/upload") public ModelAndView upload(@RequestParam("uploadFile")MultipartFile file) throws Exception { ModelAndView modelAndView = new ModelAndView(); if (!file.isEmpty()) { //1.取檔案格式字尾名 String type = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".")); //2.取當前時間戳作為檔名 String fileName = System.currentTimeMillis() + type; //3.設定存放位置 String path = "E:\\程式碼存檔\\springmvc\\springMVC_demo\\src\\main\\java\\com\\steven\\ssm\\upload\\" + fileName; //4.建立檔案 File destFile = new File(path); try { //5.複製臨時檔案到指定目錄下 //FileUtils.copyInputStreamToFile()這個方法裡對IO進行了自動操作,不需要額外的再去關閉IO流 FileUtils.copyInputStreamToFile(file.getInputStream(), destFile); } catch (IOException e) { e.printStackTrace(); } modelAndView.setViewName("items/success"); } else { modelAndView.setViewName("items/failed"); } return modelAndView; } //上傳處理(restful版) @RequestMapping(value = "/upload",method = RequestMethod.POST) @ResponseBody public WebResult upload(@RequestParam("uploadFile")MultipartFile file) throws Exception { WebResult result = WebResultHelper.newResult(); if (!file.isEmpty()) { //1.取檔案格式字尾名 String type = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".")); //2.取當前時間戳作為檔名 String fileName = System.currentTimeMillis() + type; //3.設定存放位置 String path = "E:\\程式碼存檔\\springmvc\\springMVC_demo\\src\\main\\java\\com\\steven\\ssm\\upload\\" + fileName; //4.建立檔案 File destFile = new File(path); try { //5.複製臨時檔案到指定目錄下 //FileUtils.copyInputStreamToFile()這個方法裡對IO進行了自動操作,不需要額外的再去關閉IO流 FileUtils.copyInputStreamToFile(file.getInputStream(), destFile); } catch (IOException e) { e.printStackTrace(); } result.put("message","upload success!"); } else { result.put("message","upload failed!"); } return result; } //下載檔案 //匹配的是href中的download請求 @RequestMapping(value="/download",method= RequestMethod.GET) public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException{ String downloadFilePath="E:\\程式碼存檔\\springmvc\\springMVC_demo\\src\\main\\java\\com\\steven\\ssm\\upload\\1542372933774.xls"; File file = new File(downloadFilePath); //http頭資訊 HttpHeaders headers = new HttpHeaders(); //設定編碼 String downloadFileName = new String("1542372933774.xls".getBytes("UTF-8"),"iso-8859-1"); headers.setContentDispositionFormData("attachment", downloadFileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); //MediaType:網際網路媒介型別 contentType:具體請求中的媒體型別資訊 return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED); } }
4.springmvc.xml中新增配置
<!-- 配置檔案處理 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>