gpt4 book ai didi

java.net.MalformedURLException : no protocol on URL based on a string modified with URLEncoder

转载 作者:IT老高 更新时间:2023-10-28 21:12:51 26 4
gpt4 key购买 nike

所以我试图在 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,然后替换正确的字符?

任何建议都非常感谢!

最佳答案

在将参数值连接到 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 字符串。

关于java.net.MalformedURLException : no protocol on URL based on a string modified with URLEncoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22093864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com