hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Phlip <phlip2...@gmail.com>
Subject Re: Upload a file through an HTTP form, via MultipartEntityBuilder, with a progress bar
Date Fri, 04 Oct 2013 18:11:28 GMT
> You do not need to get inside of #create. Just decorate the instance you get
> from #build. HttpEntity is an interface and lends itself well to the proxy or
> decorator pattern.

Thanks, got it.

Anecdotally, I had first tried to extend FileBody. The extended class
worked in production, but not in Junit test, which caught the infamous
NoClassDefFoundError error.

The winning code (in spectacular Java-Heresy(tm) style), is:

    public static String postFile(String fileName, String userName,
String password, String macAddress) throws Exception {

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(SERVER + "uploadFile");
        MultipartEntityBuilder builder =

        final File file = new File(fileName);
        FileBody fb = new FileBody(file);

        builder.addPart("file", fb);
        builder.addTextBody("userName", userName);
        builder.addTextBody("password", password);
        builder.addTextBody("macAddress",  macAddress);
        final HttpEntity yourEntity = builder.build();

        class ProgressiveEntity implements HttpEntity {
            public void consumeContent() throws IOException {
            public InputStream getContent() throws IOException,
                    IllegalStateException {
                return yourEntity.getContent();
            public Header getContentEncoding() {
                return yourEntity.getContentEncoding();
            public long getContentLength() {
                return yourEntity.getContentLength();
            public Header getContentType() {
                return yourEntity.getContentType();
            public boolean isChunked() {
                return yourEntity.isChunked();
            public boolean isRepeatable() {
                return yourEntity.isRepeatable();
            public boolean isStreaming() {
                return yourEntity.isStreaming();
            } // CONSIDER put a _real_ delegator into here!

            public void writeTo(OutputStream outstream) throws IOException {

                class ProxyOutputStream extends FilterOutputStream {
                     * @author Stephen Colebourne

                    public ProxyOutputStream(OutputStream proxy) {
                    public void write(int idx) throws IOException {
                    public void write(byte[] bts) throws IOException {

                    public void write(byte[] bts, int st, int end)
throws IOException {

                        // FIXME  Put your progress bar stuff here!

                        out.write(bts, st, end);

                    public void flush() throws IOException {
                    public void close() throws IOException {
                } // CONSIDER import this class (and risk more Jar File Hell)

                class ProgressiveOutputStream extends ProxyOutputStream {
                    public ProgressiveOutputStream(OutputStream proxy) {

                yourEntity.writeTo(new ProgressiveOutputStream(outstream));

        ProgressiveEntity myEntity = new ProgressiveEntity();

        HttpResponse response = client.execute(post);

        return getContent(response);


    public static String getContent(HttpResponse response) throws IOException {
        BufferedReader rd = new BufferedReader(new
        String body = "";
        String content = "";

        while ((body = rd.readLine()) != null)
            content += body + "\n";
        return content.trim();

To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org

View raw message