标签:encode pos xls 下载 tchar fileinput coding getpath form
@GetMapping(value = "/downloadTemplate") public void downFrozenTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception { String fileName = "模板"+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xlsx"; ServletOutputStream out; response.setCharacterEncoding("UTF-8"); response.setContentType("content-type:octet-stream"); //文件在项目中的存放路径 String filePath = getClass().getResource("/templates/template1.xlsx").getPath(); String userAgent = request.getHeader("User-Agent"); if (userAgent.contains("MSIE") || userAgent.contains("Trident")) { fileName = java.net.URLEncoder.encode(fileName, "UTF-8"); } else { // 非IE浏览器的处理: fileName = new String((fileName).getBytes("UTF-8"), "ISO-8859-1"); } filePath = URLDecoder.decode(filePath, "UTF-8"); response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); FileInputStream inputStream = new FileInputStream(filePath); out = response.getOutputStream(); int b = 0; byte[] buffer = new byte[1024]; while ((b = inputStream.read(buffer)) != -1) { //写到输出流(out)中 out.write(buffer, 0, b); } inputStream.close(); if (out != null) { out.flush(); out.close(); } }
标签:encode pos xls 下载 tchar fileinput coding getpath form
原文地址:https://www.cnblogs.com/zengnansheng/p/11584523.html