CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-httpcomponents--httpmime

Apache HttpComponents HttpClient MIME multipart entity support for handling multipart/form-data requests and file uploads

Pending
Overview
Eval results
Files

content-body-types.mddocs/

Content Body Types

Different content body implementations for handling various data types in multipart forms. Content bodies represent the actual content of multipart sections and provide type-specific handling for text, files, binary data, and streams.

Capabilities

ContentBody Interface

Base interface for all content body implementations, providing core functionality for multipart content.

/**
 * Base interface for multipart content bodies
 * Extends ContentDescriptor for MIME type information
 */
public interface ContentBody extends ContentDescriptor {
    /**
     * Get the filename for Content-Disposition header
     * @return Filename string or null if not applicable
     */
    String getFilename();
    
    /**
     * Write the content body to an output stream
     * @param out Output stream to write content to
     * @throws IOException If an I/O error occurs during writing
     */
    void writeTo(OutputStream out) throws IOException;
}

ContentDescriptor Interface

Provides MIME type and content metadata information for content bodies.

/**
 * Interface for content descriptors providing MIME information
 */
public interface ContentDescriptor {
    /**
     * Get the complete MIME type (e.g., "text/plain", "image/jpeg")
     * @return MIME type string, defaults to "text/plain" if not specified
     */
    String getMimeType();
    
    /**
     * Get the MIME media type (e.g., "text", "image", "application")
     * @return Media type string, defaults to "text" if not specified
     */
    String getMediaType();
    
    /**
     * Get the MIME subtype (e.g., "plain", "html", "jpeg")
     * @return Subtype string, defaults to "plain" if not specified
     */
    String getSubType();
    
    /**
     * Get the character encoding for text content
     * @return Charset string, null for non-text content or when not specified
     */
    String getCharset();
    
    /**
     * Get the transfer encoding type
     * @return Transfer encoding string, defaults to "7bit"
     */
    String getTransferEncoding();
    
    /**
     * Get the content length in bytes
     * @return Content length, or -1 if unknown
     */
    long getContentLength();
}

StringBody

Text content body implementation for string data with character encoding support.

/**
 * Text content body backed by a string/byte array
 * Suitable for form fields, JSON data, XML content, etc.
 */
public class StringBody extends AbstractContentBody {
    /**
     * Create a string body with specified content type
     * @param text Text content to include
     * @param contentType Content type (e.g., ContentType.TEXT_PLAIN, ContentType.APPLICATION_JSON)
     */
    public StringBody(String text, ContentType contentType);
    
    /**
     * Get the text content length in bytes
     * @return Content length in bytes
     */
    public long getContentLength();
    
    /**
     * String bodies do not have filenames
     * @return Always returns null
     */
    public String getFilename();
    
    /**
     * Write the text content to output stream
     * @param out Output stream to write to
     * @throws IOException If writing fails
     */
    public void writeTo(OutputStream out) throws IOException;
    
    /**
     * Get content as a Reader for character-based access
     * @return Reader for the text content
     * @throws UnsupportedEncodingException If charset is not supported
     */
    public Reader getReader() throws UnsupportedEncodingException;
}

FileBody

Binary content body implementation for file uploads with automatic content type detection and streaming support.

/**
 * Binary content body backed by a file
 * Provides efficient streaming for large files without loading into memory
 */
public class FileBody extends AbstractContentBody {
    /**
     * Create a file body with default binary content type
     * @param file File to upload
     */
    public FileBody(File file);
    
    /**
     * Create a file body with specified content type
     * @param file File to upload
     * @param contentType Content type for the file
     */
    public FileBody(File file, ContentType contentType);
    
    /**
     * Create a file body with specified content type and custom filename
     * @param file File to upload
     * @param contentType Content type for the file
     * @param filename Custom filename for Content-Disposition header
     */
    public FileBody(File file, ContentType contentType, String filename);
    
    /**
     * Get the underlying file object
     * @return File object being uploaded
     */
    public File getFile();
    
    /**
     * Get filename for Content-Disposition header
     * @return Filename (custom name if specified, otherwise file.getName())
     */
    public String getFilename();
    
    /**
     * Get file size as content length
     * @return File size in bytes
     */
    public long getContentLength();
    
    /**
     * Write file content to output stream
     * @param out Output stream to write file content to
     * @throws IOException If file reading or writing fails
     */
    public void writeTo(OutputStream out) throws IOException;
    
    /**
     * Get input stream for reading file content
     * @return FileInputStream for the file
     * @throws FileNotFoundException If file does not exist
     */
    public InputStream getInputStream() throws FileNotFoundException;
}

ByteArrayBody

Binary content body implementation for in-memory byte arrays with efficient direct access.

/**
 * Binary content body backed by a byte array
 * Suitable for small to medium binary data already in memory
 */
public class ByteArrayBody extends AbstractContentBody {
    /**
     * Create byte array body with default binary content type
     * @param data Byte array containing the binary data
     * @param filename Filename for Content-Disposition header
     */
    public ByteArrayBody(byte[] data, String filename);
    
    /**
     * Create byte array body with specified content type and filename
     * @param data Byte array containing the binary data
     * @param contentType Content type for the binary data
     * @param filename Filename for Content-Disposition header
     */
    public ByteArrayBody(byte[] data, ContentType contentType, String filename);
    
    /**
     * Get filename for Content-Disposition header
     * @return Filename specified in constructor
     */
    public String getFilename();
    
    /**
     * Get byte array length as content length
     * @return Length of the byte array
     */
    public long getContentLength();
    
    /**
     * Write byte array content to output stream
     * @param out Output stream to write array content to
     * @throws IOException If writing fails
     */
    public void writeTo(OutputStream out) throws IOException;
}

InputStreamBody

Binary content body implementation for streaming data from input streams with unknown content length.

/**
 * Binary content body backed by an input stream
 * Suitable for large data streams or when data source is not known in advance
 * Content length is typically unknown (-1)
 */
public class InputStreamBody extends AbstractContentBody {
    /**
     * Create input stream body with specified content type
     * @param in Input stream containing the binary data
     * @param contentType Content type for the stream data
     */
    public InputStreamBody(InputStream in, ContentType contentType);
    
    /**
     * Create input stream body with specified content type and filename
     * @param in Input stream containing the binary data
     * @param contentType Content type for the stream data
     * @param filename Filename for Content-Disposition header
     */
    public InputStreamBody(InputStream in, ContentType contentType, String filename);
    
    /**
     * Create input stream body with default binary content type
     * @param in Input stream containing the binary data
     * @param filename Filename for Content-Disposition header
     */
    public InputStreamBody(InputStream in, String filename);
    
    /**
     * Get the underlying input stream
     * @return Input stream containing the data
     */
    public InputStream getInputStream();
    
    /**
     * Get filename for Content-Disposition header
     * @return Filename specified in constructor, or null
     */
    public String getFilename();
    
    /**
     * Stream content length is typically unknown
     * @return Always returns -1 (unknown length)
     */
    public long getContentLength();
    
    /**
     * Write stream content to output stream
     * @param out Output stream to write stream content to
     * @throws IOException If reading from input stream or writing fails
     */
    public void writeTo(OutputStream out) throws IOException;
}

AbstractContentBody

Base implementation class providing common functionality for content body implementations.

/**
 * Abstract base class for content body implementations
 * Provides ContentDescriptor implementation based on ContentType
 */
public abstract class AbstractContentBody implements ContentBody {
    /**
     * Create abstract content body with specified content type
     * @param contentType Content type information
     */
    public AbstractContentBody(ContentType contentType);
    
    /**
     * Get the ContentType object
     * @return ContentType instance with full type information
     */
    public ContentType getContentType();
    
    // ContentDescriptor implementations
    public String getMimeType();
    public String getMediaType();
    public String getSubType();
    public String getCharset();
    
    // Abstract methods - must be implemented by subclasses
    public abstract String getTransferEncoding();
    public abstract long getContentLength();
    public abstract void writeTo(OutputStream out) throws IOException;
    public abstract String getFilename();
}

Usage Examples:

import org.apache.http.entity.mime.content.*;
import org.apache.http.entity.ContentType;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;

// Text content for form fields
StringBody username = new StringBody("john_doe", ContentType.TEXT_PLAIN);
StringBody jsonData = new StringBody("{\"type\":\"user\",\"active\":true}", 
                                     ContentType.APPLICATION_JSON);

// File uploads
FileBody document = new FileBody(new File("report.pdf"), ContentType.APPLICATION_PDF);
FileBody image = new FileBody(new File("photo.jpg"), ContentType.IMAGE_JPEG, "profile-photo.jpg");

// Binary data from arrays
byte[] imageBytes = // ... load image data
ByteArrayBody avatar = new ByteArrayBody(imageBytes, ContentType.IMAGE_PNG, "avatar.png");

// Streaming data
FileInputStream stream = new FileInputStream("large-file.zip");
InputStreamBody largeFile = new InputStreamBody(stream, ContentType.APPLICATION_OCTET_STREAM, "archive.zip");

// Custom content with specific charset
ContentType customType = ContentType.create("text/csv", StandardCharsets.UTF_8);
StringBody csvData = new StringBody("name,email\nJohn,john@example.com", customType);

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-httpcomponents--httpmime

docs

content-body-types.md

form-part-customization.md

index.md

multipart-entity-building.md

tile.json