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

时间:2014-02-28 11:26:06

标签: java uri malformedurlexception

所以我试图在URL中使用这个String: -

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

我通过谷歌搜索实现了这是由于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.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中,然后使用正确的字符替换?

任何建议都非常感谢!

5 个答案:

答案 0 :(得分:25)

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

答案 1 :(得分:4)

我有同样的问题,我用属性文件读取了网址:

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

dev.properties

url = "https://myDevServer.com/dev/api/gate"

应该是

dev.properties

url = https://myDevServer.com/dev/api/gate

没有""我的问题解决了。

根据oracle documentation

  
      
  • 抛出此异常表示发生了格式错误的URL。要么在规范字符串或字符串中找不到合法的协议   无法解析。
  •   

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

答案 2 :(得分:0)

您想使用URI templates。仔细查看该项目的自述文件:URLEncoder.encode()不适用于URI。

我们来看看您的原始网址:

  

HTTP://site-test.collercapital.com/Meetings/IC/DownloadDocument meetingId = c21c905c-8359-4bd6-b864-844709e05754&安培;的itemId = a4b724d1-282e-4b36-9d16-d619a807ba67&安培; FILE = \ s604132shvw140 \试验文档\ c21c905c-8359-4bd6-b864-844709e05754_attachments \ 7e89c3cb-ce53-4a04-a9ee-1a584e157987 \ myDoc.pdf

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

http://site-test.collercapital.com/Meetings/IC/DownloadDocument
    ?meetingId={meetingID}&itemId={itemID}&file={file}

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

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

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

这是保证返回功能齐全的URL!

答案 3 :(得分:0)

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

这不是一个不常见的问题-正确的数据(用引号引起来),人的大脑不会出现任何问题!

.....
                .addInterceptor(logging).addInterceptor(new HeaderInterceptor())
                .build();
    ..

答案 4 :(得分:0)

此代码对我有效

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

实例化的新网址:http://path