首頁 > Java > java教程 > 主體

如何使用 java.net.URLConnection 將檔案和附加參數上傳到 HTTP 伺服器?

Mary-Kate Olsen
發布: 2024-10-25 03:02:30
原創
182 人瀏覽過

How to upload files and additional parameters to an HTTP server using java.net.URLConnection?

使用 Java 中的附加參數將檔案上傳到 HTTP 伺服器

將檔案上傳到 HTTP 伺服器是許多應用程式的常見需求。但是,有時也需要隨文件一起傳遞附加參數。這是一個允許您在不使用外部程式庫的情況下發送檔案和參數的解決方案:

java.net.URLConnection 和Multipart/Form-Data

傳送檔案和參數,您將利用java.net.URLConnection 並採用multipart/form-data 編碼。 Multipart/form-data 可讓您在單一 HTTP 請求中混合二進位資料(檔案)和字元資料(參數)。

範例程式碼:

<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");
String boundary = Long.toHexString(System.currentTimeMillis());
String CRLF = "\r\n";

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 normal 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 of multipart/form-data.
    writer.append("--" + boundary + "--").append(CRLF).flush();
}

// Request is lazily fired whenever you need to obtain information about response.
int responseCode = ((HttpURLConnection) connection).getResponseCode();
System.out.println(responseCode); </code>
登入後複製

附加說明:

  • 確保為每個多部分請求唯一的邊界值。
  • 檔案必須採用發送 Content-Type 標頭時指定的字元集.
  • Apache Commons HttpComponents Client 可以進一步簡化流程,但這不是必要的。

參考:

  • [使用用於觸發和處理HTTP 請求的java.net.URLConnection](https://docs.oracle .com/javase/tutorial/networking/urls/creating-urls.html)

以上是如何使用 java.net.URLConnection 將檔案和附加參數上傳到 HTTP 伺服器?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!