2014-02-28 117 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.

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

相关问题