hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cole Kelley <ckel...@irishgrin.com>
Subject Re: Watching progress of transfer
Date Thu, 28 Feb 2008 05:58:02 GMT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body alink="#ee0000" bgcolor="#ffffff" link="#0000ee" text="#000000"
 vlink="#551a8b">
I was wondering the same thing, and set out to solve the problem.<br>
<br>
I just subclassed FilePart and overrode the sendData method.&nbsp; It
notifies a Dialog of the uploaded chunk, which in turn, updates the
progress meter. Here's the complete code for the subclass:<br>
<blockquote><small><font face="Courier New, Courier, monospace">import
java.io.File;</font></small><br>
  <small><font face="Courier New, Courier, monospace">import
java.io.FileNotFoundException;</font></small><br>
  <small><font face="Courier New, Courier, monospace">import
java.io.IOException;</font></small><br>
  <small><font face="Courier New, Courier, monospace">import
java.io.InputStream;</font></small><br>
  <small><font face="Courier New, Courier, monospace">import
java.io.OutputStream;</font></small><br>
  <small><font face="Courier New, Courier, monospace">import
org.apache.commons.httpclient.methods.multipart.*;</font></small><br>
  <br>
  <small><font face="Courier New, Courier, monospace">/**</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;* A FilePart
that notifies the calling dialog when a chunk of data has been</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;* uploaded.</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;* </font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;* @author Cole
Kelley</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;*/</font></small><br>
  <small><font face="Courier New, Courier, monospace">public class
NewFilePart extends FilePart</font></small><br>
  <small><font face="Courier New, Courier, monospace">{</font></small><br>
  <br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp; private
UploadDialog dialog;</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp; public
static
final double NANO = 1000000000.0;</font></small><br>
  <br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp; public
NewFilePart(String name, File file, UploadDialog d)</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
throws
FileNotFoundException</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp; {</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
super(name,
file);</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dialog = d;</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp; }</font></small><br>
  <br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp; @Override</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp; protected
void
sendData(OutputStream out) throws IOException</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp; {</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//LOG.trace("enter sendData(OutputStream out)");</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if
(lengthOfData() == 0)</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></small><br>
  <br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// this
file contains no data, so there is nothing to send.</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// we
don't want to create a zero length buffer as this will</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// cause
an infinite loop when reading.</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//LOG.debug("No data to send.");</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return;</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</font></small><br>
  <br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
byte[] tmp
= new byte[4096];</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
InputStream
instream = super.getSource().createInputStream();</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
try</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
int len;</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
while
((len = instream.read(tmp)) &gt;= 0)</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
long
start_time = System.nanoTime();</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
out.write(tmp, 0, len);</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//
Get nanoseconds since upload started and finished.</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
long
complete_in = System.nanoTime() - start_time;</font></small><br>
  <br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//
Grab the transfer bitrate.</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
long
Bps = Math.round(len/(complete_in/NANO));</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//
Let the dialog know a chunk is done</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dialog.notifyChunk(len, Bps);</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
} finally</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// we're
done with the stream, close it</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
instream.close();</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</font></small><br>
  <small><font face="Courier New, Courier, monospace">&nbsp;&nbsp; }</font></small><br>
  <small><font face="Courier New, Courier, monospace">}</font></small><br>
</blockquote>
Hope that helps!<br>
<br>
Cole Kelley<br>
<br>
Stephen More wrote:
<blockquote
 cite="mid:bbf4cf4f0802211109r760d487i1d80f5161f3db1f4@mail.gmail.com"
 type="cite">
  <pre wrap="">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: <a class="moz-txt-link-abbreviated" href="mailto:httpclient-users-unsubscribe@hc.apache.org">httpclient-users-unsubscribe@hc.apache.org</a>
For additional commands, e-mail: <a class="moz-txt-link-abbreviated" href="mailto:httpclient-users-help@hc.apache.org">httpclient-users-help@hc.apache.org</a>


  </pre>
</blockquote>
<br>
</body>
</html>

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


Mime
View raw message