阿里雲 oss 檔案下載進度顯示
思路是:第一獲取要下載檔案的大小,設為X,第二通過多執行緒監測已下載檔案的大小,設為Y,Y/x就是下載的進度。關鍵程式碼如下
//獲取要下載的object的metadata
ObjectMetadata tmpMetadata = client.getObjectMetadata(bucketName, key);
final long filesize = tmpMetadata.getContentLength();
final File downloadFile = new File(filename);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(downloadFile.getName()+"下載的進度為:"+ (float)downloadFile.length()*100/filesize);
if(downloadFile.length()==filesize)
break;
}
}
});
t.start();
//下載object
ObjectMetadata result =client.getObject(new GetObjectRequest(bucketName, key),downloadFile);