我需要上传文件到服务器并监视它的进度。 我需要得到一个通知每次发送多少个字节。如何监视文件上传进度?
例如,在下载的情况下,我有:
HttpURLConnection connection = (HttpURLConnection) m_url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
while ((currentBytes = stream.read(byteBuffer)) > 0) {
totalBytes+=currentBytes;
//calculate something...
}
现在我需要上传这样做。但如果使用
OutputStreamWriter stream = new OutputStreamWriter(connection.getOutputStream());
stream.write(str);
stream.flush();
比我无法获得任何progres通知,关于发送多少字节(它看起来像自动操作)。
有什么建议吗?
感谢
这是一个耻辱,你没有得到这个问题的真实答案,我很好奇...... – MirroredFate 2011-06-13 15:37:48
看来,HttpURLConnection不是任务的正确对象。它对输出没有阻塞,所以我无法用它来衡量性能。我正在尝试使用套接字。 – Sophie 2011-06-16 13:12:54
是的,使用套接字,您可以使用getSendBufferSize来确定它们何时发送数据。 – MirroredFate 2011-06-16 14:51:45