home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (MultipartEntity.java)

This example source code file (MultipartEntity.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java tags/keywords

charset, contentbody, formbodypart, header, httpmultipart, httpmultipartmode, io, ioexception, iterator, message, multipartentity, random, string, stringbuilder, unsupportedoperationexception, util

The MultipartEntity.java example source code

 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *   http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.

package org.apache.http.entity.mime;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.http.annotation.ThreadSafe;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.james.mime4j.field.Fields;
import org.apache.james.mime4j.message.Message;

 * Multipart/form coded HTTP entity consisting of multiple
 * body parts. 
 * @since 4.0
public class MultipartEntity implements HttpEntity {

     * The pool of ASCII chars to be used for generating a multipart boundary.
    private final static char[] MULTIPART_CHARS = 
    private final Message message;
    private final HttpMultipart multipart;
    private final Header contentType;
    private long length;
    private volatile boolean dirty; // used to decide whether to recalculate length
    public MultipartEntity(
            HttpMultipartMode mode, 
            final String boundary,
            final Charset charset) {
        this.multipart = new HttpMultipart("form-data");
        this.contentType = new BasicHeader(
                generateContentType(boundary, charset));
        this.dirty = true;
        this.message = new Message();
        org.apache.james.mime4j.message.Header header = 
          new org.apache.james.mime4j.message.Header();
        if (mode == null) {
            mode = HttpMultipartMode.STRICT;

    public MultipartEntity(final HttpMultipartMode mode) {
        this(mode, null, null);

    public MultipartEntity() {
        this(HttpMultipartMode.STRICT, null, null);

    protected String generateContentType(
            final String boundary,
            final Charset charset) {
        StringBuilder buffer = new StringBuilder();
        buffer.append("multipart/form-data; boundary=");
        if (boundary != null) {
        } else {
            Random rand = new Random();
            int count = rand.nextInt(11) + 30; // a random size from 30 to 40
            for (int i = 0; i < count; i++) {
        if (charset != null) {
            buffer.append("; charset=");
        return buffer.toString();

    public void addPart(final String name, final ContentBody contentBody) {
        this.multipart.addBodyPart(new FormBodyPart(name, contentBody));
        this.dirty = true;
    public boolean isRepeatable() {
        List<?> parts = this.multipart.getBodyParts();
        for (Iterator<?> it = parts.iterator(); it.hasNext(); ) {
            FormBodyPart part = (FormBodyPart) it.next();
            ContentBody body = (ContentBody) part.getBody();
            if (body.getContentLength() < 0) {
                return false;
        return true;

    public boolean isChunked() {
        return !isRepeatable();

    public boolean isStreaming() {
        return !isRepeatable();

    public long getContentLength() {
        if (this.dirty) {
            this.length = this.multipart.getTotalLength();
            this.dirty = false;
        return this.length;

    public Header getContentType() {
        return this.contentType;

    public Header getContentEncoding() {
        return null;

    public void consumeContent()
        throws IOException, UnsupportedOperationException{
        if (isStreaming()) {
            throw new UnsupportedOperationException(
                    "Streaming entity does not implement #consumeContent()");

    public InputStream getContent() throws IOException, UnsupportedOperationException {
        throw new UnsupportedOperationException(
                    "Multipart form entity does not implement #getContent()");

    public void writeTo(final OutputStream outstream) throws IOException {


Other Java examples (source code examples)

Here is a short list of links related to this Java MultipartEntity.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.