如何使用 Java 发出多部分/表单数据 POST 请求? [英] How can I make a multipart/form-data POST request using Java?

查看:34
本文介绍了如何使用 Java 发出多部分/表单数据 POST 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Apache Commons HttpClient 3.x 版本的时代,可以进行 multipart/form-data POST 请求(2004 年的示例).不幸的是,这在 4.0 版 HttpClient 中不再可能.

In the days of version 3.x of Apache Commons HttpClient, making a multipart/form-data POST request was possible (an example from 2004). Unfortunately this is no longer possible in version 4.0 of HttpClient.

对于我们的核心活动HTTP",multipart 有点超出范围.我们很乐意使用由某些人维护的多部分代码它在范围内的其他项目,但我不知道任何.我们尝试将多部分代码移至 commons-codec 几年以前,但我没有在那里起飞.奥列格最近提到了另一个具有多部分解析代码并且可能感兴趣的项目在我们的多部分格式化代码中.我不知道现在的状态在那.(http://www.nabble.com/multipart-form-data-in-4.0-td14224819.html)

For our core activity "HTTP", multipart is somewhat out of scope. We'd love to use multipart code maintained by some other project for which it is in scope, but I'm not aware of any. We tried to move the multipart code to commons-codec a few years ago, but I didn't take off there. Oleg recently mentioned another project that has multipart parsing code and might be interested in our multipart formatting code. I don't know the current status on that. (http://www.nabble.com/multipart-form-data-in-4.0-td14224819.html)

有没有人知道任何允许我编写 HTTP 客户端的 Java 库,该客户端可以发出多部分/表单数据 POST 请求?

Is anybody aware of any Java library that allows me to write an HTTP client that can make a multipart/form-data POST request?

背景:我想使用远程Zoho Writer API.

推荐答案

我们使用 HttpClient 4.x 进行多部分文件发布.

We use HttpClient 4.x to make multipart file post.

更新:从 HttpClient 4.3 开始,一些类已被弃用.这是带有新 API 的代码:

UPDATE: As of HttpClient 4.3, some classes have been deprecated. Here is the code with new API:

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost("...");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN);

// This attaches the file to the POST:
File f = new File("[/path/to/upload]");
builder.addBinaryBody(
    "file",
    new FileInputStream(f),
    ContentType.APPLICATION_OCTET_STREAM,
    f.getName()
);

HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
HttpEntity responseEntity = response.getEntity();

以下是带有不推荐使用的 HttpClient 4.0 API 的原始代码片段:

Below is the original snippet of code with deprecated HttpClient 4.0 API:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

FileBody bin = new FileBody(new File(fileName));
StringBody comment = new StringBody("Filename: " + fileName);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

这篇关于如何使用 Java 发出多部分/表单数据 POST 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆