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

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

Susan Sarandon
Release: 2024-12-09 14:35:19
Original
324 people have browsed it

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

Making a Multipart/Form-Data POST Request in Java

HTTP's multipart/form-data encoding is commonly used for file uploads, allowing clients to send both text and binary data in a single request. While Apache Commons HttpClient 3.x supported this functionality, it was removed in version 4.0. This article explores alternative Java libraries that enable multipart/form-data POST requests.

Despite reaching out to the HttpClient project, there are no known efforts to re-implement multipart support. As such, developers must seek alternative solutions for their multipart/form-data needs.

Solution

HttpClient 4.x, the current stable version, offers a more modern API for handling multipart requests. Here's an example using the updated API:

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost("...");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN);
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

For developers still using HttpClient 4.0 (not recommended), the following code snippet uses the deprecated 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();
Copy after login

Other Java libraries that support multipart/form-data POST requests include:

  • Apache HTTP Components HttpComponents-Client: A modern, thread-safe HTTP client library
  • Jersey Client API: A high-level, easy-to-use RESTful web services client library
  • Grizzly REST Framework: A framework for building RESTful web services

The above is the detailed content of How to Make Multipart/Form-Data POST Requests in Java?. 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