Home  >  Article  >  Java  >  Detailed explanation of the solution to garbled file names in Java downloads

Detailed explanation of the solution to garbled file names in Java downloads

尚
Original
2019-12-03 15:49:071954browse

Detailed explanation of the solution to garbled file names in Java downloads

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn