Java の「URLConnection」を使用してファイルと追加パラメータをアップロードするにはどうすればよいですか?

Barbara Streisand
リリース: 2024-10-25 02:18:02
オリジナル
210 人が閲覧しました

How to Upload Files and Additional Parameters with Java's `URLConnection`?

追加パラメータを使用して Java クライアントから HTTP サーバーにファイルをアップロードする方法

HTTP POST リクエストを使用して Java クライアントからサーバーにファイルをアップロードする場合、一般的にファイルと一緒に追加のパラメータを含めたい場合。外部ライブラリを必要としないシンプルで効率的なソリューションは次のとおりです。

java.net.URLConnection を利用して HTTP リクエストを確立し、マルチパート フォーム データ用に設定します。これは、バイナリとバイナリの両方を処理するための一般的なエンコード形式です。テキストデータ。追加のパラメータ param とファイル textFile および binaryFile を含む例を次に示します:

<code class="java">String url = "http://example.com/upload";
String charset = "UTF-8";
String param = "value";
File textFile = new File("/path/to/file.txt");
File binaryFile = new File("/path/to/file.bin");

URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

try (
    OutputStream output = connection.getOutputStream();
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
    // Send param
    writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
    writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
    writer.append(CRLF).append(param).append(CRLF).flush();

    // Send text file
    writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);
    writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
    writer.append(CRLF).flush();
    Files.copy(textFile.toPath(), output);
    output.flush();
    writer.append(CRLF).flush();

    // Send binary file
    writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
    writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
    writer.append("Content-Transfer-Encoding: binary").append(CRLF);
    writer.append(CRLF).flush();
    Files.copy(binaryFile.toPath(), output);
    output.flush();
    writer.append(CRLF).flush();

    // End boundary
    writer.append("--" + boundary + "--").append(CRLF).flush();
}</code>
ログイン後にコピー

リクエストを設定した後、リクエストを起動して応答コードを取得できます:

<code class="java">((HttpURLConnection) connection).getResponseCode();</code>
ログイン後にコピー

詳細高度なシナリオを使用するか、プロセスを簡素化するには、Apache Commons HttpComponents Client などのサードパーティ ライブラリの使用を検討してください。

以上がJava の「URLConnection」を使用してファイルと追加パラメータをアップロードするにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!