2014-02-28 60 views
16

所以我試圖用這個字符串的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,然後使用正確的字符替換掉的內容。

任何建議非常感謝!

+0

'URLEncoder.encode()'不適用於URI!你想要[URI模板](https://github.com/fge/uri-template)< - 這個庫可以提供幫助。 – fge

回答

21

您需要在將參數值鏈接到URL之前對其進行編碼。
反斜槓\是必須被轉義爲%5C

例如轉義特殊字符:

String paramValue = "param\\with\\backslash"; 
String yourURLStr = "http://host.com?param=" + java.net.URLEncoder.encode(paramValue, "UTF-8"); 
java.net.URL url = new java.net.URL(yourURLStr); 

結果是http://host.com?param=param%5Cwith%5Cbackslash其格式正確的URL字符串。

+0

感謝您對特殊字符的提醒!我在文件中加入了'fileToDownloadLocation = fileToDownloadLocation.replace(「\\」,「%5C」);'和嘿presto一切都很好,很好又簡單的修復了我,歡呼! :) – MorkPork

+1

這不適用於帶空格的文件...同樣,URLEncoder.encode()不適用於URI! – fge

+0

空格將被替換爲加號「+」字符。這是適當的URL轉義。 –

0

您想使用URI templates。仔細查看該項目的自述文件:URLEncoder.encode()對於URIs不起作用。

讓我們把你的原始網址:

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

,並與兩個變量轉換爲URI模板(多線清晰度):

http://site-test.collercapital.com/Meetings/IC/DownloadDocument 
    ?meetingId={meetingID}&itemId={itemID}&file={file} 

現在讓我們建立一個變量地圖這三個變量使用鏈接中提到的庫:

final VariableMap = VariableMap.newBuilder() 
    .addScalarValue("meetingID", "c21c905c-8359-4bd6-b864-844709e05754") 
    .addScalarValue("itemID", "a4b724d1-282e-4b36-9d16-d619a807ba67e") 
    .addScalarValue("file", "\\\\s604132shvw140\\Test-Documents" 
     + "\\c21c905c-8359-4bd6-b864-844709e05754_attachments" 
     + "\\7e89c3cb-ce53-4a04-a9ee-1a584e157987\\myDoc.pdf") 
    .build(); 

final URITemplate template 
    = new URITemplate("http://site-test.collercapital.com/Meetings/IC/DownloadDocument" 
     + "meetingId={meetingID}&itemId={itemID}&file={file}"); 

// Generate URL as a String 
final String theURL = template.expand(vars); 

這是保證D返回一個功能齊全的網址!

+0

感謝您的詳細回覆,我假設如果我想實現這一點,我不得不從github等下載我將調查的東西,歡呼! – MorkPork

3

我有同樣的問題,我讀的URL與屬性文件:

String configFile = System.getenv("system.Environment"); 
     if (configFile == null || "".equalsIgnoreCase(configFile.trim())) { 
      configFile = "dev.properties"; 
     } 
     // Load properties 
     Properties properties = new Properties(); 
     properties.load(getClass().getResourceAsStream("/" + configFile)); 
     //read url from file 
     apiUrl = properties.getProperty("url").trim(); 
      URL url = new URL(apiUrl); 
      //throw exception here 
    URLConnection conn = url.openConnection(); 

開發。性能

url = "https://myDevServer.com/dev/api/gate" 

應該

dev.properties

url = https://myDevServer.com/dev/api/gate 

沒有 「」 我的問題就解決了。

據甲骨文documentation

  • Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.

因此,這意味着它是不是裏面的字符串被解析。

相關問題