java.net.MalformedURLException:基于使用URLEncoder修改的字符串,URL上没有协议 [英] java.net.MalformedURLException: no protocol on URL based on a string modified with URLEncoder

查看:1273
本文介绍了java.net.MalformedURLException:基于使用URLEncoder修改的字符串,URL上没有协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在URL中使用这个字符串: -

So I was attempting to use this String in a URL :-

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());

但此时我收到错误: -

But at this point I get the error: -

java.net.URISyntaxException: Illegal character in query at index 169:Blahblahblah

我通过一些谷歌搜索意识到这是由于URL中的字符(猜测&),所以我添加了一些代码所以现在看起来像这样: -

I realised with a bit of googling this was due to the characters in the URL (guessing the &), so I then added in some code so it now looks like so: -

String fileToDownloadLocation = //The above string
fileToDownloadLocation = URLEncoder.encode(fileToDownloadLocation, "UTF-8");
URL fileToDownload = new URL(fileToDownloadLocation);
HttpGet httpget = new HttpGet(fileToDownload.toURI());

然而,当我尝试运行它时,当我尝试创建URL时出现错误,然后错误读取: -

However, when I try and run this I get an error when I try and create the URL, the error then reads: -

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之后才能进行编码,否则它会替换斜杠和不应该的东西,但是我看不出如何用字符串创建URL然后格式化它以便它适合使用。我对这一切并不是特别熟悉,并且希望有人能够指出我错过了将字符串A放入适当格式化的URL然后使用更换的正确字符?

It looks like I can't do the encoding until after I've created the URL else it replaces slashes and things which it shouldn't, but I can't see how I can create the URL with the string and then format it so its suitable for use. I'm not particularly familiar with all this and was hoping someone might be able to point out to me what I'm missing to get string A into a suitably formatted URL to then use with the correct characters replaced?

任何建议都非常感谢!

推荐答案

您需要对参数的值进行编码,然后再连接它们URL。

反斜杠 \ 是特殊字符,必须转义为%5C

You need to encode your parameter's values before concatenating them to URL.
Backslash \ is special character which have to be escaped as %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字符串。

The result is http://host.com?param=param%5Cwith%5Cbackslash which is properly formatted url string.

这篇关于java.net.MalformedURLException:基于使用URLEncoder修改的字符串,URL上没有协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆