首页  >  文章  >  Java  >  如何在不依赖第三方库的情况下使用纯Java将文件和参数上传到HTTP服务器?

如何在不依赖第三方库的情况下使用纯Java将文件和参数上传到HTTP服务器?

Patricia Arquette
Patricia Arquette原创
2024-10-25 04:11:29785浏览

How can I upload files and parameters to an HTTP server using pure Java without relying on third-party libraries?

使用 Java 的 HTTP 服务器文件上传

简介

本文讨论将文件上传到来自 Java 客户端的 HTTP 服务器。我们的目标是提供一个简单且免费的解决方案,在 POST 请求中组合参数和文件。

多部分/表单数据编码

对于混合 POST 内容(二进制和二进制)字符数据),通常使用多部分/表单数据编码。数据分为多个部分,每个部分都有自己的标头和正文。

Java 解决方案

以下代码演示了如何使用直接 HTTP 连接上传文件和参数没有第三方库:

<code class="java">// Parameters
String url = "http://example.com/upload";
String param = "value";

// File paths
File textFile = new File("/path/to/file.txt");
File binaryFile = new File("/path/to/file.bin");

// Generate unique boundary value
String boundary = Long.toHexString(System.currentTimeMillis());

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

// OutputStream for writing data
try (OutputStream output = connection.getOutputStream();
     PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true)) {

    // Write parameter
    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();

    // Write 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); // Ensure file is saved using this charset
    writer.append(CRLF).flush();
    Files.copy(textFile.toPath(), output);
    output.flush(); 
    writer.append(CRLF).flush();

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

// Get HTTP response code
int responseCode = ((HttpURLConnection) connection).getResponseCode();
System.out.println(responseCode);</code>

附加说明

  • Files.copy 在 Java 7 中引入;对于旧版本,请使用 FileUtils.copyFile。
  • Apache Commons HttpComponents Client 是一个流行的第三方库,可以简化此过程。
  • Apache Commons FileUpload 用于在服务器端处理文件上传。

另请参阅

  • [使用 java.net.URLConnection 触发和处理 HTTP 请求](资源链接)

以上是如何在不依赖第三方库的情况下使用纯Java将文件和参数上传到HTTP服务器?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn