Apache HttpComponents HttpClient MIME multipart entity support for handling multipart/form-data requests and file uploads
—
Advanced form part creation with custom headers and fine-grained control over multipart section properties. This provides low-level access to multipart form construction for scenarios requiring custom headers or special formatting.
Creates and configures individual form body parts with custom headers and properties.
/**
* Builder for individual form body parts with custom header support
*/
public class FormBodyPartBuilder {
/**
* Create a form body part builder with name and content body
* @param name Field name for the form part
* @param body Content body implementation for the part data
* @return New FormBodyPartBuilder instance for method chaining
*/
public static FormBodyPartBuilder create(String name, ContentBody body);
/**
* Create an empty form body part builder
* @return New FormBodyPartBuilder instance for method chaining
*/
public static FormBodyPartBuilder create();
/**
* Set the field name for the form part
* @param name Field name to use in Content-Disposition header
* @return This builder instance for method chaining
*/
public FormBodyPartBuilder setName(String name);
/**
* Set the content body for the form part
* @param body Content body implementation containing the part data
* @return This builder instance for method chaining
*/
public FormBodyPartBuilder setBody(ContentBody body);
/**
* Add a custom header field to the form part
* @param name Header field name
* @param value Header field value
* @return This builder instance for method chaining
*/
public FormBodyPartBuilder addField(String name, String value);
/**
* Set a header field, replacing any existing field with the same name
* @param name Header field name
* @param value Header field value
* @return This builder instance for method chaining
*/
public FormBodyPartBuilder setField(String name, String value);
/**
* Remove all header fields with the specified name
* @param name Header field name to remove
* @return This builder instance for method chaining
*/
public FormBodyPartBuilder removeFields(String name);
/**
* Build the configured form body part
* @return FormBodyPart instance ready for use in multipart entities
*/
public FormBodyPart build();
}Represents a complete form part with headers, name, and content body for inclusion in multipart entities.
/**
* Individual form body part containing headers, name, and content
* Automatically populates standard headers based on content body properties
*/
public class FormBodyPart {
/**
* Get the field name for this form part
* @return Field name used in Content-Disposition header
*/
public String getName();
/**
* Get the content body for this form part
* @return ContentBody implementation containing the part data
*/
public ContentBody getBody();
/**
* Get the header collection for this form part
* @return Header object containing all headers for this part
*/
public Header getHeader();
/**
* Add a header field to this form part
* @param name Header field name
* @param value Header field value
*/
public void addField(String name, String value);
}Container and utilities for managing MIME headers within form parts.
/**
* Container for MIME headers in form parts
* Supports multiple values for the same header name
*/
public class Header {
/**
* Add a header field to the collection
* @param field MinimalField containing name and value
*/
public void addField(MinimalField field);
/**
* Get all header fields
* @return List of all MinimalField instances
*/
public List<MinimalField> getFields();
/**
* Get the first header field with the specified name
* @param name Header field name to search for
* @return First matching MinimalField or null if not found
*/
public MinimalField getField(String name);
/**
* Get all header fields with the specified name
* @param name Header field name to search for
* @return List of all matching MinimalField instances
*/
public List<MinimalField> getFields(String name);
/**
* Remove all header fields with the specified name
* @param name Header field name to remove
* @return Number of fields removed
*/
public int removeFields(String name);
/**
* Set a header field, replacing any existing fields with the same name
* @param field MinimalField to set (replaces existing fields with same name)
*/
public void setField(MinimalField field);
/**
* Iterate over all header fields
* @return Iterator for all MinimalField instances
*/
public Iterator<MinimalField> iterator();
}Individual header field implementation for name-value pairs in MIME headers.
/**
* Individual MIME header field with name and value
*/
public class MinimalField {
/**
* Create a header field with name and value
* @param name Header field name
* @param value Header field value
*/
public MinimalField(String name, String value);
/**
* Get the header field name
* @return Header field name
*/
public String getName();
/**
* Get the header field value
* @return Header field value
*/
public String getBody();
/**
* Get formatted header field as "name: value"
* @return Formatted header field string
*/
public String toString();
}Utility class containing standard MIME header names and encoding constants.
/**
* MIME-related constants and utilities
*/
public class MIME {
// Header name constants
public static final String CONTENT_TYPE = "Content-Type";
public static final String CONTENT_TRANSFER_ENC = "Content-Transfer-Encoding";
public static final String CONTENT_DISPOSITION = "Content-Disposition";
// Transfer encoding constants
public static final String ENC_8BIT = "8bit";
public static final String ENC_BINARY = "binary";
// Character set constants
public static final Charset DEFAULT_CHARSET; // US-ASCII
public static final Charset UTF8_CHARSET; // UTF-8
}Usage Examples:
import org.apache.http.entity.mime.*;
import org.apache.http.entity.mime.content.*;
import org.apache.http.entity.ContentType;
import java.io.File;
// Basic custom form part with additional headers
FormBodyPart customPart = FormBodyPartBuilder
.create("document", new FileBody(new File("report.pdf"), ContentType.APPLICATION_PDF))
.addField("Content-Description", "Monthly Sales Report")
.addField("X-Document-Type", "financial")
.addField("X-Department", "sales")
.build();
// Text part with custom headers
StringBody jsonBody = new StringBody("{\"version\":\"1.0\",\"format\":\"report\"}",
ContentType.APPLICATION_JSON);
FormBodyPart metadataPart = FormBodyPartBuilder
.create("metadata", jsonBody)
.addField("Content-Description", "Report Metadata")
.addField("X-Schema-Version", "2.1")
.build();
// Image with custom disposition and headers
ByteArrayBody imageBody = new ByteArrayBody(imageBytes, ContentType.IMAGE_JPEG, "profile.jpg");
FormBodyPart imagePart = FormBodyPartBuilder
.create("avatar", imageBody)
.addField("Content-Description", "User Profile Picture")
.addField("X-Image-Source", "camera")
.addField("X-Compression", "high")
.build();
// Use custom parts in multipart entity
HttpEntity entity = MultipartEntityBuilder.create()
.addPart(customPart)
.addPart(metadataPart)
.addPart(imagePart)
.addTextBody("username", "johndoe") // Standard parts can be mixed in
.build();
// Access part properties
String partName = customPart.getName(); // "document"
ContentBody body = customPart.getBody(); // FileBody instance
Header headers = customPart.getHeader();
// Work with headers directly
Header customHeaders = new Header();
customHeaders.addField(new MinimalField("Content-Language", "en-US"));
customHeaders.addField(new MinimalField("X-Priority", "high"));
customHeaders.addField(new MinimalField("X-Department", "engineering"));
// Get header values
MinimalField priority = customHeaders.getField("X-Priority");
List<MinimalField> allFields = customHeaders.getFields();
String priorityValue = priority != null ? priority.getBody() : "normal";
// Complex form part with streaming content and multiple headers
FileInputStream largeFileStream = new FileInputStream("large-dataset.csv");
InputStreamBody streamBody = new InputStreamBody(largeFileStream,
ContentType.create("text/csv"),
"dataset.csv");
FormBodyPart streamPart = FormBodyPartBuilder
.create("data", streamBody)
.addField("Content-Description", "Machine Learning Dataset")
.addField("X-Data-Format", "CSV")
.addField("X-Record-Count", "1000000")
.addField("X-Columns", "name,age,score,category")
.addField("X-Encoding", "UTF-8")
.build();Install with Tessl CLI
npx tessl i tessl/maven-org-apache-httpcomponents--httpmime