所以我試圖用這個字符串的URL修改的字符串: -java.net.MalformedURLException:在URL沒有協議基於與URLEncoder的
http://site-test.collercapital.com/Meetings/IC/DownloadDocument?meetingId=c21c905c-8359-4bd6-b864-844709e05754&itemId=a4b724d1-282e-4b36-9d16-d619a807ba67&file=\\s604132shvw140\Test-Documents\c21c905c-8359-4bd6-b864-844709e05754_attachments\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf
在此代碼: -
String fileToDownloadLocation = //The above string
URL fileToDownload = new URL(fileToDownloadLocation);
HttpGet httpget = new HttpGet(fileToDownload.toURI());
但在這一點上,我得到的錯誤: -
java.net.URISyntaxException: Illegal character in query at index 169:Blahblahblah
我有點谷歌上搜索這個的實現是由於UR人物L(猜&),所以後來我在一些代碼加入所以現在看起來像這樣: -
String fileToDownloadLocation = //The above string
fileToDownloadLocation = URLEncoder.encode(fileToDownloadLocation, "UTF-8");
URL fileToDownload = new URL(fileToDownloadLocation);
HttpGet httpget = new HttpGet(fileToDownload.toURI());
然而,當我嘗試和運行此我得到一個錯誤,當我試圖創建URL時,然後錯誤讀取: -
java.net.MalformedURLException: no protocol: http%3A%2F%2Fsite-test.collercapital.com%2FMeetings%2FIC%2FDownloadDocument%3FmeetingId%3Dc21c905c-8359-4bd6-b864-844709e05754%26itemId%3Da4b724d1-282e-4b36-9d16-d619a807ba67%26file%3D%5C%5Cs604132shvw140%5CTest-Documents%5Cc21c905c-8359-4bd6-b864-844709e05754_attachments%5C7e89c3cb-ce53-4a04-a9ee-1a584e157987%myDoc.pdf
它看起來像我不能這樣做,直到編碼後,我創建了網址否則它取代了斜線和事物,它不應該,但我看不出我可以使用字符串創建URL,然後將其格式化爲適合使用的格式。我對這一切並不是特別熟悉,並希望有人能夠向我指出我錯過了將字符串A轉換爲適當格式的URL,然後使用正確的字符替換掉的內容。
任何建議非常感謝!
'URLEncoder.encode()'不適用於URI!你想要[URI模板](https://github.com/fge/uri-template)< - 這個庫可以提供幫助。 – fge