[英]Java Progress Bar Help
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 现在,我如何知道已下载多少文件来更新进度条
That's not easy since Services.downloadFile()
doesn't give you the file in chunks. 这并不容易,因为
Services.downloadFile()
不会分块地提供文件。 If you had a loop, it would be simple. 如果您有一个循环,那将很简单。
If you can change the API, I suggest to change it in this way: 如果您可以更改API,建议您通过以下方式进行更改:
Services.downloadFile(File, OutputStream);
and let downloadFile()
write the file into the stream. 并让
downloadFile()
将文件写入流中。 Now you can wrap the stream and count the bytes written by overloading the various versions of write()
. 现在,您可以包装流并通过重载各个版本的
write()
计数写入的字节。
[EDIT] I saw that Services
is an RMI proxy. [编辑]我看到
Services
是RMI代理。 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. byte[] read()
方法。 read()
method returns the content of the buffer and clears it. read()
方法返回缓冲区的内容并清除它。 On the client: 在客户端上:
read()
on the download object on the server. read()
。 The API is something like read(key)
, the server looks up the key in the map and then calls read()
on the download object. read(key)
,服务器在映射中查找密钥,然后在下载对象上调用read()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.