简体   繁体   English

Java进度栏帮助

[英]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: 因此,在您的服务器上,您需要:

  1. Create an object that handles the download and which has a byte[] read() method. 创建一个处理下载的对象,该对象具有byte[] read()方法。
  2. Put the objects into a map with a key. 使用键将对象放入地图。 Send the key to the client somehow. 以某种方式将密钥发送给客户端。
  3. For each object, create a thread that actually downloads the data and adds the data to a buffer. 对于每个对象,创建一个实际下载数据并将数据添加到缓冲区的线程。 The read() method returns the content of the buffer and clears it. read()方法返回缓冲区的内容并清除它。

On the client: 在客户端上:

  1. You need to call a method on the server to create a new download object and start the download thread. 您需要在服务器上调用一个方法来创建新的下载对象并启动下载线程。
  2. Now you need a loop that calls 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. 该API类似于read(key) ,服务器在映射中查找密钥,然后在下载对象上调用read()
  3. Show the progress as you write the result of the read calls to the file 将读取调用的结果写入文件时显示进度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM