[jsp]中文檔名處理

JSP中文檔名的部分,若要下載之類的總是有些小問題,那通常問題出在header


CODE
response.setContentType("application/octet-stream; charset=UTF-8");
if (request.getHeader("User-Agent").indexOf("MSIE") != -1) {
// IE 8 9 10
response.setHeader("Content-Disposition", "attachment; filename=\"" + java.net.URLEncoder.encode(filename, "UTF-8") + "\"");
} else if (request.getHeader("User-Agent").indexOf("Trident") != -1) {
//IE11
response.setHeader("Content-Disposition", "attachment; filename=\"" + java.net.URLEncoder.encode(filename, "UTF-8") + "\"");
} else {
//firefox 中文檔名處理
filename = java.net.URLDecoder.decode(filename, "UTF-8");
response.setHeader("Content-disposition", "attachment; filename=\"" + new String(filename.getBytes("UTF-8"), "ISO8859-1") + "\"");
}

常用類型需要其他的自行GOOGLE
.* = application/octet-stream
.doc = application/msword
.xsl = text/xml
.zip - application/zip

目前測過
IE 8 9 10 11
FF
CHROME
SAFARI
Android Stock Browser

有問題歡迎留言之類的

這個網誌中的熱門文章

[java]BIG5 字集判斷

[java]半型轉全型

[javascript]當月第一天、當月最後一天