下載的空格變成+, 括號變成亂碼-------處理文件名包含%20 不轉換成空格

如果下載的文件名稱包括空格、(、)、;、@、#、&

 String newName = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20").replaceAll("%28", "\\(").replaceAll("%29", "\\)").replaceAll("%3B", ";").replaceAll("%40", "@").replaceAll("%23", "\\#").replaceAll("%26", "\\&");

如果下載的文件名稱包括%20 chrome 瀏覽器回解析成空格

            // response.setContentType("." + URLEncoder.encode((newName), "UTF-8") + "; charset=UTF-8");
            response.setContentType(".dump9; charset=UTF-8");
            if (browserType != null && browserType.equals("GOOGLE_CHROME")) {
                response.setHeader("Content-disposition", "attachment; filename*=UTF-8''" + newName);  //處理文件名包含%20 不轉換成空格
            } else {
                response.setHeader("Content-disposition", "attachment; filename=" + newName);
            }
            //response.setContentType("application/x-msdownload;");
            response.setContentType("application/dmp;");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Content-Length", String.valueOf(size));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章