首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Java发出多部分/表单数据POST请求?

如何使用Java发出多部分/表单数据POST请求?
EN

Stack Overflow用户
提问于 2009-09-04 12:27:53
回答 13查看 328.3K关注 0票数 106

在Apache Commons HttpClient版本3.x的时代,可以发出多部分/表单数据POST请求(an example from 2004)。不幸的是,这在version 4.0 of HttpClient中不再是可能的。

对于我们的核心活动“

”,multipart有点超出了范围。我们很乐意使用由其他项目维护的多部分代码,但我不知道有没有。几年前,我们试图将多部分代码转移到commons-codec,但我并没有从那里开始。奥列格最近提到了另一个项目,它有多部分解析代码,可能会对我们的多部分格式化代码感兴趣。我不知道它现在的状态。(http://www.nabble.com/multipart-form-data-in-4.0-td14224819.html)

有没有人知道有什么Java库可以让我编写一个可以发出多部分/表单数据POST请求的HTTP客户端?

背景:我想使用Remote API of Zoho Writer

EN

回答 13

Stack Overflow用户

回答已采纳

发布于 2009-09-04 12:43:05

我们使用HttpClient 4.x来制作多部分文件post。

更新:从HttpClient 4.3开始,一些类已经被弃用。下面是新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的原始代码片段

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();
票数 159
EN

Stack Overflow用户

发布于 2010-06-09 14:00:46

这些是我拥有的Maven依赖项。

Java代码:

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

FileBody uploadFilePart = new FileBody(uploadFile);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("upload-file", uploadFilePart);
httpPost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httpPost);

Pom.xml中的Maven依赖项:

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.0.1</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.0.1</version>
  <scope>compile</scope>
</dependency>
票数 39
EN

Stack Overflow用户

发布于 2013-04-27 00:36:05

如果JAR的大小很重要(例如,在applet的情况下),也可以直接将httpmime与java.net.HttpURLConnection一起使用,而不是HttpClient。

httpclient-4.2.4:      423KB
httpmime-4.2.4:         26KB
httpcore-4.2.4:        222KB
commons-codec-1.6:     228KB
commons-logging-1.1.1:  60KB
Sum:                   959KB

httpmime-4.2.4:         26KB
httpcore-4.2.4:        222KB
Sum:                   248KB

代码:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");

FileBody fileBody = new FileBody(new File(fileName));
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
multipartEntity.addPart("file", fileBody);

connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue());
OutputStream out = connection.getOutputStream();
try {
    multipartEntity.writeTo(out);
} finally {
    out.close();
}
int status = connection.getResponseCode();
...

Pom.xml中的依赖项:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.2.4</version>
</dependency>
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1378920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档