java ie 亂碼怎麼辦?Java 使用IE瀏覽器下載文件,文件名亂碼問題
推薦教程:《java學習》
使用Servlet實作檔案下載功能時,使用IE下載出現檔案名稱亂碼;
網路上常見的解決方案是透過"user-agen "來判斷瀏覽器:
if (req.getHeader("user-agent").toLowerCase().contains("msie")) { // IE filename = URLEncoder.encode(filename, "UTF-8"); } else { // 非IE filename = new String(filename.getBytes("UTF-8"), "iso-8859-1"); }
但是在Windows 10 中用戶代理字串已經修改了 不在是"msie"了
相容("相容")和瀏覽器("MSIE" ) 令牌已刪除。
"like Gecko" 令牌已新增(以便與其他瀏覽器一致)。
瀏覽器版本現在由新版本 ("rv") 令牌報告。
win 10 ie user-agent Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko ie 11 win 10 edge user-agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393 edge
所以就不能在使用msie判斷是否是IE 瀏覽器.
String userAgent = req.getHeader("user-agent").toLowerCase(); if (userAgent.contains("msie") || userAgent.contains("like gecko") ) { // win10 ie edge 浏览器 和其他系统的ie fileName = URLEncoder.encode(fileName, "UTF-8"); } else { // fe fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); }
專案範例程式碼:
try { String fileName = attachmentFile.originalName(); String userAgent = request.getHeader("user-agent").toLowerCase(); if (userAgent.contains("msie") || userAgent.contains("like gecko") ) { // win10 ie edge 浏览器 和其他系统的ie fileName = URLEncoder.encode(fileName, "UTF-8"); } else { // fe fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1"); } response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); String path = attachmentFile.path(); InputStream inputStream = new FileInputStream(new File(path)); OutputStream os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } // 这里主要关闭。 os.close(); inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
以上是java ie 亂碼怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
刺客信條陰影:貝殼謎語解決方案
3 週前ByDDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
2 週前ByDDD
在哪裡可以找到原子中的起重機控制鑰匙卡
3 週前ByDDD
節省R.E.P.O.解釋(並保存文件)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影 - 如何找到鐵匠,解鎖武器和裝甲定制
4 週前ByDDD

熱工具

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。