The name of the chrome Firefox download file is garbled.

[background] requirements require that the exported user information Excel table file be downloaded from the server to the user locally
[code]

   /**
    * /
    * @param request
    * @param response
    * @return 
    * @throws FTPConnectionClosedException
    * @throws IOException
    */
   public File downloadExcel (HttpServletRequest request, HttpServletResponse response)
                throws FTPConnectionClosedException, IOException {
    File file = new File(zipPath);
    FileOutputStream fos = new FileOutputStream(file);
    ZipUtils.toZip(path, fos, true);
    //1.
    String realPath = zipPath;
    //2.
    String fileName = realPath.substring(realPath.lastIndexOf(File.separator)+1);
    response.reset();
    response.setCharacterEncoding("UTF-8");
    response.setContentType("multipart/form-data");
    //3.content-disposition
    response.addHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes(),"utf-8"));
    //
    InputStream in = new FileInputStream(realPath);
    int len = 0;
    byte[] buffer = new byte[1024];
    OutputStream out = response.getOutputStream();
    while ((len = in.read(buffer)) > 0) {
          //
        out.write(buffer,0,len);
    }
    in.close();
         return file;
   }

[question] how to convert garbled code into normal Chinese?

clipboard.png

Feb.14,2022

try one: https://codeshelper.com/a/11. setting the HTTP header field Content-Disposition according to the standard in this link does not work;


fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setContentType("application/octet-stream;charset=UTF-8");  

try it this way.


    response.addHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes(),"utf-8"));

try changing utf-8 to iso8859-1

Menu