java.net.MalformedURLException:基于使用URLEncoder修改的字符串的URL上没有协议

java.net.MalformedURLException:基于使用URLEncoder修改的字符串的URL上没有协议,java,uri,malformedurlexception,Java,Uri,Malformedurlexception,因此,我试图在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_attachm

因此,我试图在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,然后在替换正确字符的情况下使用

非常感谢您的任何建议

您想要使用。请仔细阅读此项目的自述文件:
urlcoder.encode()
不适用于URI

让我们以您的原始URL为例:

\测试文件\c21c905c-8359-4bd6-b864-844709e05754_附件\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf

并将其转换为具有两个变量的URI模板(为了清晰起见,在多行上):

现在,让我们使用链接中提到的库使用这三个变量构建一个变量映射:

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.test.com/Meetings/IC/DownloadDocument"
        + "meetingId={meetingID}&itemId={itemID}&file={file}");

// Generate URL as a String
final String theURL = template.expand(vars);

这保证返回一个功能齐全的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字符串。

我也有同样的问题,我用属性文件读取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"
url = https://myDevServer.com/dev/api/gate
应该是

开发属性

url = "https://myDevServer.com/dev/api/gate"
url = https://myDevServer.com/dev/api/gate
没有“”我的问题就解决了

根据甲骨文

  • 抛出以指示已发生格式错误的URL。在规范字符串或字符串中找不到合法协议 无法分析

这意味着它没有在字符串中解析。

多亏了Erhun的回答,我终于意识到我的JSON映射器也在返回数据周围的引号!我需要使用“asText()”而不是“toString()”

这不是一个罕见的问题——一个人的大脑不会发现正确数据有任何错误,周围都是引号

discoveryJson.path("some_endpoint").toString();
"https://what.the.com/heck"

discoveryJson.path("some_endpoint").asText();
https://what.the.com/heck

这个代码对我有效

public static void main(String[] args) {
    try {
        java.net.URL myUr = new java.net.URL("http://path");
        System.out.println("Instantiated new URL: " + connection_url);
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

实例化的新URL:

URLEncoder.encode()
不适用于URI!你想要感谢关于特殊角色的提醒!我在
fileToDownloadLocation=fileToDownloadLocation.replace(“\\”,“%5C”)行中添加了嘿,普雷斯托一切又好了,很好很简单的解决办法让我度过难关,干杯!:)这不适用于带有空格的文件。。。同样,URLEncoder.encode()不适用于URI!空格将替换为加号“+”字符。这是正确的URL转义。感谢详细的回复,我想如果我想实现这一点,我必须从github等下载我将调查的内容,干杯!同样的问题,但是从
导出\u URL=“”