gpt4 book ai didi

java - 将文件从 Java 客户端上传到 HTTP 服务器

转载 作者:IT老高 更新时间:2023-10-28 13:53:10 27 4
gpt4 key购买 nike

我想将一些文件上传到 HTTP 服务器。基本上我需要的是某种带有一些参数和文件的对服务器的 POST 请求。我已经看到了仅上传文件的示例,但没有找到如何传递其他参数。

这样做最简单且免费的解决方案是什么?有人有我可以研究的文件上传示例吗?我已经在谷歌上搜索了几个小时,但是(也许这只是其中的一天)找不到我需要的确切内容。最好的解决方案是不涉及任何第三方类或库。

最佳答案

您通常会使用 java.net.URLConnection触发 HTTP 请求。您通常也会使用 multipart/form-data混合 POST 内容(二进制和字符数据)的编码。单击链接,它包含信息和如何编写 multipart/form-data 请求正文的示例。该规范在 RFC2388 中有更详细的描述。 .

这是一个启动示例:

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()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.

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); // Text file itself must be saved in this charset!
writer.append(CRLF).flush();
Files.copy(textFile.toPath(), output);
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

// 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(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

// 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); // Should be 200

当您使用 Apache Commons 之类的第 3 方库时,此代码不那么冗长 HttpComponents Client .

Apache Commons FileUpload正如一些人错误地暗示的那样,这里只对服务器端感兴趣。在客户端你不能使用也不需要它。

另见

关于java - 将文件从 Java 客户端上传到 HTTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2469451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com