How to download garbled file names in java: (Recommended: java video tutorial)
Each browser has different encoding, Firefox uses What is base64.ie and Google use url encoding:
So when we set the response header
Content-Type getServletContext().getMineType(filename); Content-Disposition accachment;filename=编码后的filename
url encoding
name = URLEncoder.encode(filename, "UTF-8");
base64 Coding
/** 传入文件的名称,进行Base64编码@param fileName@return */ public String base64EncodeFileName(String fileName) { BASE64Encoder base64Encoder = new BASE64Encoder(); try { return "=?UTF-8?B?"new String(base64Encoder.encode(fileName .getBytes("UTF-8"))) + "?="; } catch (UnsupportedEncodingException e) { e.printStackTrace(); throw new RuntimeException(e); } }
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of Detailed explanation of the solution to garbled file names in Java downloads. For more information, please follow other related articles on the PHP Chinese website!