基於springmvc,poi處處excel文件

POI導出excelspring

springmvc上傳以及下載瀏覽器

//上傳文件會自動綁定到MultipartFile中mvc

@RequestMapping(value="/upload",method=RequestMethod.POST)app

public String upload(HttpServletRequest request,excel

@RequestParam("description") String description,orm

@RequestParam("file") MultipartFile file) throws Exception {圖片

System.out.println(description); //若是文件不爲空,寫入上傳路徑ip

if(!file.isEmpty()) {get

//上傳文件路徑it

String path = request.getServletContext().getRealPath("/images/");

//上傳文件名 String filename = file.getOriginalFilename();

File filepath = new File(path,filename);

//判斷路徑是否存在,若是不存在就建立一個

if (!filepath.getParentFile().exists())

{ filepath.getParentFile().mkdirs(); }

//將上傳文件保存到一個目標文件當中

file.transferTo(new File(path + File.separator + filename)); return "success"; } else { return "error"; }

}

//下載文件SpringMVC提供了一個ResponseEntity類型,使用它能夠很方便地定義返回的HttpHeaders和HttpStatus

@RequestMapping(value="/download")

public ResponseEntity<byte[]> download(HttpServletRequest request,

@RequestParam("filename") String filename,

Model model)throws Exception

{ //下載文件路徑

String path = request.getServletContext().getRealPath("/images/");

File file = new File(path + File.separator + filename); HttpHeaders headers = new HttpHeaders();

//下載顯示的文件名,解決中文名稱亂碼問題

String downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1");

//通知瀏覽器以attachment(下載方式)打開圖片

headers.setContentDispositionFormData("attachment", downloadFielName);

//application/octet-stream : 二進制流數據(最多見的文件下載)

headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),

headers, HttpStatus.CREATED); }

相關文章
相關標籤/搜索