创建包含分段文件上传的发布请求

发布于 2024-08-15 12:23:49 字数 705 浏览 4 评论 0原文

我正在编写一个简单的代码片段,它发送一个简单的发布请求。

目前,我正在像这样构建请求:

    // Construct data
    String data = URLEncoder.encode("param1", "UTF-8") + "=" + URLEncoder.encode("val1", "UTF-8");
    data += "&" + URLEncoder.encode("param2", "UTF-8") + "=" + URLEncoder.encode("val2", "UTF-8");

    // Send data
    URL url = new URL("http://server:8080/servlet/upload");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // do stuff with response....

到目前为止,这是有效的。但我需要添加文件上传作为多部分 POST 请求。 我该怎么做?如果可能的话,我想避免使用公共资源中的 HttpClient

I am writing a simple snippet which sends a simple post request.

Currently I am building the request like so:

    // Construct data
    String data = URLEncoder.encode("param1", "UTF-8") + "=" + URLEncoder.encode("val1", "UTF-8");
    data += "&" + URLEncoder.encode("param2", "UTF-8") + "=" + URLEncoder.encode("val2", "UTF-8");

    // Send data
    URL url = new URL("http://server:8080/servlet/upload");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // do stuff with response....

This works, as of now. But I need to add a file upload as a multipart POST request.
How can I do this? I would like to avoid using HttpClient from commons if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神魇的王 2024-08-22 12:23:50

试试这个,因为这对我的情况有效

File f = new File(filePath);
PostMethod filePost = new PostMethod(url);
Part[] parts = { new FilePart("file", f) };
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
HttpClient client = new HttpClient();
status = client.executeMethod(filePost);
logger.info("upload status: " + status);

Try this as this worked in my case

File f = new File(filePath);
PostMethod filePost = new PostMethod(url);
Part[] parts = { new FilePart("file", f) };
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
HttpClient client = new HttpClient();
status = client.executeMethod(filePost);
logger.info("upload status: " + status);
热情消退 2024-08-22 12:23:49

目前您根本没有使用 HTTP。如果您打算执行 POST,您需要做的第一件事是确保发送正确的标头等,以便您实际上正在进行 HTTP 连接。然后你需要遵循 RFC 1867 ( https://www.rfc-editor.org/rfc/ rfc1867 )将文件内容正确编码到您的 POST 中。这并不容易,这就是为什么有一些库可以为您做到这一点。所以我不得不问:为什么要避免使用 HttpClient?我一直用它来达到这个目的。它可靠、完整且高性能。您的(内存/磁盘)空间不足吗?

Currently you aren't using HTTP at all. If you intend to do a POST, the first thing you need to do is make sure you send the correct headers and such, so you are actually engaging in an HTTP connection. Then you need to follow RFC 1867 ( https://www.rfc-editor.org/rfc/rfc1867 ) to properly encode the file contents into your POST. This is not easy, which is why there are libraries out there which do this for you. So I have to ask: why avoid HttpClient? I've always used it for this purpose. It's reliable, complete and performant. Are you short on (memory/disk) space?

薄情伤 2024-08-22 12:23:49

到目前为止,您需要使用 RFC 1687 和 RFC 2388。这是一项繁重的工作,我不会发布启动代码示例,抱歉:) 然而,RFC 包含清晰的信息和数据应如何显示的几个示例。这是绝对可行的。

To the point, you need to construct an outputstream with data in the format as specified in RFC 1687 and RFC 2388. It's a lot of work, I am not going to post a kickoff code example, sorry :) The RFC however contains clear information and several examples how the data should look like. It is absolutely doable.

つ低調成傷 2024-08-22 12:23:49

此代码片段对我很有帮助: 上传 。

它没有任何外部依赖项,只有大约 150 行代码(包括注释),恕我直言,它比 Apache HttpClient 库更容易处理

This code snippet served me well: Upload files by sending multipart request programmatically

It does not have any external dependencies, is only ~ 150 lines of code including comments and is IMHO even easier to handle than the Apache HttpClient library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文