情景:下载文件时中文文件名未编码,识别失败,尤其是中文问题:
//UrlEncode编码
private String encodeUrlChinese(String fileName){
char[] str = fileName.toCharArray();
StringBuilder sb = new StringBuilder();
int strSize = fileName.length();
char ch;
for (int i=0,j=0; i<strSize; ++i) {
ch = str[i];
if (((ch>='A') && (ch<'Z')) ||
((ch>='a') && (ch<'z')) ||
((ch>='0') && (ch<'9'))) {
sb.append(ch);
} else if (ch == ' ') {
sb.append("%20");
} else if (ch == '.' || ch == '-' || ch == '_' || ch == '*') {
sb.append(ch);
} else {
try {
sb.append(URLEncoder.encode(ch + "", "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
//调用:
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");