38

だから私は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 を作成するまでエンコーディングを実行できないようです使用に適しています。私はこれらすべてに特に精通していません。文字列 A を適切にフォーマットされた URL に変換し、正しい文字を置き換えて使用するために欠けているものを誰かが指摘してくれることを期待していましたか?

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

4

6 に答える 6

29

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 文字列です。

于 2014-02-28T12:10:24.450 に答える
8

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

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

オラクルのドキュメントによると

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

したがって、文字列内で解析されないことを意味します。

于 2014-05-30T14:06:23.783 に答える
2

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-d619a807ba67&file=\s604132shvw140\Test-Documents\59c38-c215 _ -4bd6-b864-844709e05754_attachments\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf

そして、それを 2 つの変数を持つ URI テンプレートに変換します (わかりやすくするために複数の行があります)。

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

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

これは、完全に機能する URL を返すことが保証されています!

于 2014-02-28T11:56:24.527 に答える
1

このコードは私のために働いた

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

インスタンス化された新しい URL: http://path

于 2020-06-09T07:10:35.227 に答える