簡體   English   中英

java.net.MalformedURLException: 沒有基於 URLEncoder 修改的字符串的 URL 協議

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

所以我試圖在 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 字符串。

我有同樣的問題,我用屬性文件讀取了 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

沒有“”,我的問題就解決了。

根據oracle 文檔

  • 拋出以指示出現格式錯誤的 URL。 在規范字符串中找不到合法協議或無法解析字符串。

所以這意味着它沒有在字符串內解析。

您想使用URI 模板 仔細查看該項目的 README: URLEncoder.encode()不適用於 URI。

讓我們獲取您的原始 URL:

http://site-test.test.com/Meetings/IC/DownloadDocument?meetingId=c21c905c-8359-4bd6-b864-844709e05754&itemId=a4b724d1-282e-4b36-9d16-d61970s10c\\Documentation\\Documentation\\Documentation\\205c905c-8359-4bd6-b864-844709e05754&itemId -4bd6-b864-844709e05754_attachments\\7e89c3cb-ce53-4a04-a9ee-1a584e157987\\myDoc.pdf

並將其轉換為具有兩個變量的 URI 模板(為清晰起見,在多行中):

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

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

這是保證返回一個功能齊全的 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: http://path

非常簡單的修復

String encodingURL = UriUtils.encodePath(request.getUrl(), "UTF-8");

無需額外功能即可工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM