如何监控文件上传进度? [英] How to monitor file upload progress?
问题描述
我需要将文件上传到服务器并监控其进度。
i需要通知每次发送多少字节。
I need to upload a file to server and monitor it's progress. i need to get a notification how many bytes are sent each time.
例如,在下载的情况下,我有:
For example in case of download i have:
HttpURLConnection connection = (HttpURLConnection) m_url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
while ((currentBytes = stream.read(byteBuffer)) > 0) {
totalBytes+=currentBytes;
//calculate something...
}
现在我需要做上传相同。但是如果使用
Now i need to do the same for upload. but if use
OutputStreamWriter stream = new OutputStreamWriter(connection.getOutputStream());
stream.write(str);
stream.flush();
比我无法得到任何进程通知,关于发送多少字节(看起来像automic行动)。
than i can't get any progres notification, about how many bytes are sent (it looks like automic action).
有任何建议吗?
谢谢
推荐答案
我使用过ChannelSocket对象。此对象允许在访问服务器时执行阻塞读写操作。因此,我使用此套接字模拟HTTP,并且每个写入请求都被阻止,直到它完成。这样我就可以跟随写入事务的实际进度。
I have used ChannelSocket object. This object allows to do blocking read and write when accessing the server. So i emulated HTTP using this socket and each write request was blocked untill it was done. This way i could follow the actual progress of write transaction.
这篇关于如何监控文件上传进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!