I was wondering the same thing, and set out to solve the problem.

I just subclassed FilePart and overrode the sendData method.  It notifies a Dialog of the uploaded chunk, which in turn, updates the progress meter. Here's the complete code for the subclass:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.httpclient.methods.multipart.*;

/**
 * A FilePart that notifies the calling dialog when a chunk of data has been
 * uploaded.
 *
 * @author Cole Kelley
 */
public class NewFilePart extends FilePart
{

   private UploadDialog dialog;
   public static final double NANO = 1000000000.0;

   public NewFilePart(String name, File file, UploadDialog d)
           throws FileNotFoundException
   {
      super(name, file);
      dialog = d;
   }

   @Override
   protected void sendData(OutputStream out) throws IOException
   {
      //LOG.trace("enter sendData(OutputStream out)");
      if (lengthOfData() == 0)
      {

         // this file contains no data, so there is nothing to send.
         // we don't want to create a zero length buffer as this will
         // cause an infinite loop when reading.
         //LOG.debug("No data to send.");
         return;
      }

      byte[] tmp = new byte[4096];
      InputStream instream = super.getSource().createInputStream();
      try
      {
         int len;
         while ((len = instream.read(tmp)) >= 0)
         {
            long start_time = System.nanoTime();
            out.write(tmp, 0, len);
           
            // Get nanoseconds since upload started and finished.
            long complete_in = System.nanoTime() - start_time;

            // Grab the transfer bitrate.
            long Bps = Math.round(len/(complete_in/NANO));
            // Let the dialog know a chunk is done
            dialog.notifyChunk(len, Bps);
         }
      } finally
      {
         // we're done with the stream, close it
         instream.close();
      }
   }
}
Hope that helps!

Cole Kelley

Stephen More wrote:
commons-FileUpload has a ProgressListener to display bytes transferred.

Utilities like wget provide a progress bar as well.

Can httpclient provide similiar information ( either now or in the future ) ?


-Thanks
Stephen More

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


  

--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to