所以我试图在URL中使用这个字符串:
http://site-test.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
通过一番谷歌搜索,我意识到这是由于URL中的字符(猜测是&)引起的问题,因此我添加了一些代码,现在它看起来像这样:
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.testsite.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之前我无法进行编码,否则它会替换斜线和其他不应该被替换的内容,但我又不知道如何使用字符串创建URL,然后对其进行格式化使其适用。我对这一切并不是特别熟悉,希望有人能指出我错过了什么,以便将字符串A转换为适当格式的URL,然后用正确的字符替换。
非常感谢任何建议!
URLEncoder.encode()
不适用于 URI!你需要使用 URI 模板 <-- 这个库可以帮助你。 - fge