模板下载
function downloadExcelTemplate_Click(){var url = "downloadExcelTemplate.do?isdebug=true";location.href = url;
}
/*** 下載導入模板** @param request* @param response* @return*/@RequestMapping(value = "/downloadExcelTemplate.do", method = RequestMethod.GET, produces = "application/json; charset=utf-8")@ResponseBodypublic ResponseModel downloadExcelTemplate(HttpServletRequest request, HttpServletResponse response) {ResponseModel responseModel = new ResponseModel();responseModel.setStatusCode("fail");String header = request.getHeader("User-Agent").toUpperCase();String fileName = "z中文";try {if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {fileName = URLEncoder.encode(fileName, "utf-8");fileName = fileName.replace("+", "%20"); //IE下載文件名空格變+號問題 }String filepath = request.getSession().getServletContext().getRealPath("/") +"excel/excel_route.xlsx";InputStream is = new FileInputStream(filepath);OutputStream os = setRespone(fileName, response);int length = 1024;int readLength = 0;byte buf[] = new byte[1024];readLength = is.read(buf, 0, length);while (readLength != -1) {os.write(buf, 0, readLength);readLength = is.read(buf, 0, length);}os.flush();os.close();responseModel.setStatusCode("success");} catch (UnsupportedEncodingException e) {getLogger().error("AccountInputController.downloadExcelTemplate method UnsupportedEncodingException :" + e.getMessage(), e);} catch (FileNotFoundException e) {getLogger().error("AccountInputController.downloadExcelTemplate method FileNotFoundException :" + e.getMessage(), e);} catch (Exception e) {getLogger().error("AccountInputController.downloadExcelTemplate method Exception :" + e.getMessage(), e);}return responseModel;} /*** 設置輸出響應下載流** @param fileName* @param response* @return* @throws IOException*/private OutputStream setRespone(String fileName, HttpServletResponse response) throws IOException {//輸出流OutputStream os = response.getOutputStream();//重置輸出流 response.reset();response.setContentType("application/vnd.ms-excel");//設置響應標題>這里瀏覽器會提示用戶選擇下載文件需要存放的路徑>//后續生成的文件在輸出流關閉后>action返回detailExcel進result指定響應內容為excel>自動寫入該excel到該用戶指定的路徑中response.setHeader("Content-disposition", "attachment; fileName=" + new String((fileName + ".xlsx").getBytes(), "iso8859-1"));//response.setHeader("Content-disposition", "attachment; fileName=" + URLEncoder.encode(brow,"utf-8")+".xls");(不可用)return os;}
?
轉載于:https://www.cnblogs.com/acme6/p/9237079.html
總結
- 上一篇: CODE[VS] 1275有鱼的声音
- 下一篇: 使用telnet模拟邮件的收发