文件明定義
RFC的標(biāo)準(zhǔn)做法是按照如下格式:
Content-Disposition: attachment;
filename="$encoded_fname";
filename*=utf-8''$encoded_fname
其中, $encoded_fname 指的是將 UTF-8 編碼的原始文件名按照 RFC 3986 進(jìn)行百分號(hào)編碼(percent encoding)后得到的
java代碼示例:
outputStream = response.getOutputStream();
String name = " 麥寶直播間配置文件";
String disposition = "attachment;filename*=utf-8'zh_cn'" + URLEncoder.encode(name, "utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", disposition);
空格encode之后變成+號(hào)的問(wèn)題
需要在encode之后,手動(dòng)對(duì)+號(hào)替換,替換回urlencode的空格,也就是%20
fileName.replaceAll("+","%20");