文件下载解决中文文件名无法显示问题

中文文件名需要用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;
}

猜你喜欢

转载自blog.csdn.net/lq1083301982/article/details/86592499