中文文件名需要用URL编码 public static HttpServletResponse download(String path, HttpServletResponse response) { try { File file = new File(path); String filename = file.getName(); InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); //response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),"iso-8859-1")); response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream;charset=utf-8"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } return response; }
文件下载解决中文文件名无法显示问题
最新推荐文章于 2024-08-20 10:37:23 发布