思路是:第一获取要下载文件的大小,设为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);
时间: 2024-08-04 18:35:50