Java 特殊字元URL轉義

來源:互聯網
上載者:User

    URL中如果包含的特殊字元,比如#、+、&等,如果不對其進行轉義,則通過Http不能進行下載(Client通過Get方法訪問HttpServer)。

   比如URL為  :http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love +can_the verse.mp3

   如果不對 +  進行轉義,在HttpServer中收到GET命令後,有一個對uri進行decode的操作(當然如果不執行decode操作,應該就可以不用轉義了,這裡是因為有些URL進行的encode的操作,所有必須decode),decode會把  +  
轉為空白格,再根據decode後的uri在HttpServer裡面拿資料,由於HttpServer裡面是有加號的,所有會報http404資源不存在的錯誤。解決辦法是在decode之前將 加號用“%2b”代替,decode會把“%2b”轉為 +,再去get,就可以了。

    Dongle中整個對URL的處理是:

    1、取原始URL:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love +can_the verse.mp3

    2、對原始URL特殊字元進行轉義:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love %2bcan_the verse.mp3

   3、點擊播放時給Dongle設定的URL為:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love %2bcan_the verse.mp3

  4、Dongle向手機HttpServer取資料的URL為:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love +can_the verse.mp3

  5、HttpServer對收到的URL轉義:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love %2bcan_the verse.mp3

  6、HttpServer對轉義後的URL進行decode,得到:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love +can_the verse.mp3

  7、Dongle成功下載並播放。

好像繞了一圈,沒事找事一樣。如果沒有decode這一個過程,完全可以不用這一輪的東西啊。可是圖片的URL為了相容中文名稱,必須進行encode,所有到Dongle向HttpServer GET的時候就需要對URL進行decode了。也可以在decode的時候判斷GET的是不是圖片,如果不是圖片就不decode,這樣應該也可以吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.