web-dev-qa-db-ja.com

java.net.MalformedURLException:URLEncoderで変更された文字列に基づくURLのプロトコルはありません

だから私はこの文字列を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());

しかし、この時点でエラーが発生します:-

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に入れて正しい文字を置き換えて使用するために不足しているものを誰かが指摘できることを望んでいましたか?

どんな提案も大歓迎です!

28
MorkPork

パラメーターの値を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文字列になります。

25
m-szalik

私は同じ問題を抱えています、プロパティファイルで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();

dev.properties

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

そのはず

dev.properties

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

「」なしで、私の問題は解決しました。

Oracleによると ドキュメント

  • 不正な形式のURLが発生したことを示すためにスローされます。仕様文字列に正当なプロトコルが見つからなかったか、文字列を解析できませんでした。

つまり、文字列内で解析されないことを意味します。

4
erhun

Erhunの回答のおかげで、ついにJSONマッパーがデータの周りに引用符を返していることに気付きました! 「toString()」の代わりに「asText()」を使用する必要がありました

それは珍しい問題ではありません-引用符で囲まれた正しいデータに問題はありません!

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

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

RI templates を使用します。このプロジェクトのREADMEを注意深く見てください:URLEncoder.encode()はURIでは機能しません。

元の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

そして、2つの変数を持つURIテンプレートに変換します(明確にするために複数行で):

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

リンクに記載されているライブラリを使用して、これら3つの変数で変数マップを作成します。

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を返すことが保証されています!

0
fge