亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

手機txt文件打開亂碼修復方法 txt文檔亂碼怎么修復( 二 )


recipients implementing this specificationMUST support the character sets "ISO-8859-1" and "UTF-8".并且在[RFC 5987] 3.2.1規(guī)定,百分號編碼遵從 RFC 3986.section 2.1中的定義,摘錄如下:
A percent-encoding mechanism is used to represent a data octet in acomponent when that octet's corresponding character is outside theallowed set or is being used as a delimiter of, or within, thecomponent.A percent-encoded octet is encoded as a charactertriplet, consisting of the percent character "%" followed by the twohexadecimal digits representing that octet's numeric value.Forexample, "%20" is the percent-encoding for the binary octet"00100000" (ABNF: %x20), which in US-ASCII corresponds to the spacecharacter (SP).Section 2.4 describes when percent-encoding anddecoding is applied.注意了,[RFC 3986] 明確規(guī)定了空格 會被百分號編碼為%20
而在另一份文檔 RFC 1866.Section 8.2.1 The form-urlencoded Media Type 中卻規(guī)定:
The default encoding for all forms is `application/x-www-form-urlencoded'. A form data set is represented in this media type asfollows:1. The form field names and values are escaped: spacecharacters are replaced by `+', and then reserved charactersare escaped as per [URL]這里要求application/x-www-form-urlencoded類型的消息中,空格要被替換為+,其他字符按照[URL]中的定義來轉義,其中的[URL]指向的是RFC 1738 而它的修訂版中和 URL 有關的最新文檔恰恰就是 [RFC 3986]
這也就是為什么很多文檔中描述空格(white space)的百分號編碼結果都是 +或%20,如:
w3schools:URL encoding normally replaces a space with a plus (+) sign or with %20.
MDN:Depending on the context, the character ‘ ‘ is translated to a ‘+’ (like in the percent-encoding version used in an application/x-www-form-urlencoded message), or in ‘%20’ like on URLs.
那么問題來了,開發(fā)過程中,對于空格符的百分號編碼我們應該怎么處理?
課代表建議大家遵循最新文檔,因為 [RFC 1866] 中定義的情況僅適用于application/x-www-form-urlencoded類型,就百分號編碼的定義來說,我們應該以 [RFC 3986] 為準,所以,任何需要百分號編碼的地方,都應該將空格符 百分號編碼為%20,stackoverflow 上也有支持此觀點的答案:When to encode space to plus (+) or %20?
3. 代碼實踐有了理論基礎,代碼寫起來就水到渠成了,直接上代碼:
@GetMapping("/downloadFile")public String download(String serverFileName, HttpServletRequest request, HttpServletResponse response) throws IOException {request.setCharacterEncoding("utf-8");response.setContentType("application/octet-stream");String clientFileName = fileService.getClientFileName(serverFileName);// 對真實文件名進行百分號編碼String percentEncodedFileName = URLEncoder.encode(clientFileName, "utf-8").replaceAll("\+", "%20");// 組裝contentDisposition的值StringBuilder contentDispositionValue = https://www.520longzhigu.com/diannao/new StringBuilder();contentDispositionValue.append("attachment; filename=").append(percentEncodedFileName).append(";").append("filename*=").append("utf-8''").append(percentEncodedFileName);response.setHeader("Content-disposition",contentDispositionValue.toString());// 將文件流寫到response中try (InputStream inputStream = fileService.getInputStream(serverFileName);OutputStream outputStream = response.getOutputStream()) {IOUtils.copy(inputStream, outputStream);}return "OK!";}代碼很簡單,其中有兩點需要說明一下:
URLEncoder.encode(clientFileName, “utf-8”)方法之后,為什么還要.replaceAll(“\+”, “%20″) 。正如前文所述,我們已經明確,任何需要百分號編碼的地方,都應該把 空格符編碼為 %20,而URLEncoder這個類的說明上明確標注其會將空格符轉換為+:The space character ” ” is converted into a plus sign “{@code +}”.其實這并不怪 JDK,因為它的備注里說明了其遵循的是application/x-www-form-urlencoded( PHP 中也有這么一個函數,也是這么個套路)Translates a string into {@code application/x-www-form-urlencoded} format using a specific encoding scheme. This method uses the所以這里我們用.replaceAll(“\+”, “%20”) 把+號處理一下,使其完全符合 [RFC 3986] 的百分號編碼規(guī)范 。這里為了方便說明問題,把所有操作都展現出來了 。當然,你完全可以自己實現一個PercentEncoder類,豐儉由人 。[RFC 6266] 標準中filename=的value是不需要編碼的,這里的filename=后面的 value 為什么要百分號編碼?回顧 [RFC 6266] 文檔,filename和filename*同時出現時取后者,瀏覽器太老不支持新標準時取前者 。目前主流的瀏覽器都采用自升級策略,所以大部分都支持新標準——除了老版本IE 。老版本的IE對 value 的處理策略是 進行百分號解碼 并使用 。所以這里專門把filename=的value進行百分號編碼,用來兼容老版本 IE 。PS:課代表實測 IE11 及 Edge 已經支持新標準了 。4. 瀏覽器測試根據下圖 statcounter 統(tǒng)計的 2019 年中國市場瀏覽器占有率,課代表設計了一個包含中文,英文,空格的文件名 下載-down test .txt用來測試


以上關于本文的內容,僅作參考!溫馨提示:如遇健康、疾病相關的問題,請您及時就醫(yī)或請專業(yè)人士給予相關指導!

「愛刨根生活網」www.malaban59.cn小編還為您精選了以下內容,希望對您有所幫助: