所以我试图用这个字符串的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