Home > Java > javaTutorial > How to Make Multipart/Form-Data POST Requests in Java Using HttpClient?

How to Make Multipart/Form-Data POST Requests in Java Using HttpClient?

Mary-Kate Olsen
Release: 2024-12-16 11:34:16
Original
839 people have browsed it

How to Make Multipart/Form-Data POST Requests in Java Using HttpClient?

Making a Multi-Part Form Data Post Request with Java

The advent of Apache Commons HttpClient version 4.0 eliminated the ability to perform multipart/form-data POST requests that were possible in version 3.x. The HttpClient team explained that multipart functionality fell outside the scope of their core activities and recommended seeking alternative libraries.

Finding a Suitable Java Library

The question arises as to which Java libraries offer multipart/form-data POST request capabilities. One such library is HttpClient 4.x, which provides a comprehensive solution for making these requests.

Updated HttpClient 4.3 Code

With HttpClient versions 4.3 and above, the API has been updated and some classes have been deprecated. The following code snippet illustrates the revised method for making multipart file posts:

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();
Copy after login

Deprecated HttpClient 4.0 Code (for reference)

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();
Copy after login

With these libraries, developers can seamlessly create HTTP clients capable of making multipart/form-data POST requests, enabling them to interact with Web services and APIs that require this type of request format.

The above is the detailed content of How to Make Multipart/Form-Data POST Requests in Java Using HttpClient?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template