Java 进度条帮助
我需要为以下任务延迟实现一个进度条
FileOutputStream Fos= new FileOutputStream(FilePath);
byte buffer[]=Services.downloadFile(FilePath);
Fos.write(buffer);
现在,我如何知道文件已下载了多少来更新进度条
I need to implement a progress bar for the following task delay
FileOutputStream Fos= new FileOutputStream(FilePath);
byte buffer[]=Services.downloadFile(FilePath);
Fos.write(buffer);
Now, how can i know how much the file has been downloaded to update the progress bar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不容易,因为
Services.downloadFile()
不会以块的形式提供文件。如果你有一个循环,那就很简单了。如果你可以改变API,我建议这样改变:
并让
downloadFile()
将文件写入流中。现在,您可以包装流并通过重载各个版本的write()
来计算写入的字节数。[编辑]我看到
Services
是一个RMI代理。在这种情况下,你不能使用上面的方法。相反,它变得更加复杂:您的服务器上需要一个下载池(想想“地图”)。该池允许您为每次下载请求另一个数据块。
因此,在您的服务器上,您需要:
byte[] read()
方法。read()
方法返回缓冲区的内容并将其清除。在客户端:
read()
。该 API 类似于read(key)
,服务器在映射中查找密钥,然后对下载对象调用read()
。That's not easy since
Services.downloadFile()
doesn't give you the file in chunks. If you had a loop, it would be simple.If you can change the API, I suggest to change it in this way:
and let
downloadFile()
write the file into the stream. Now you can wrap the stream and count the bytes written by overloading the various versions ofwrite()
.[EDIT] I saw that
Services
is an RMI proxy. In this case, you can't use the approach above. Instead it gets more complex:You need a pool of downloads on your server (think "Map"). The pool allows you to request another block of data for each download.
So on your server, you need:
byte[] read()
method.read()
method returns the content of the buffer and clears it.On the client:
read()
on the download object on the server. The API is something likeread(key)
, the server looks up the key in the map and then callsread()
on the download object.