プログレスバーを使用して、アップロードまたはダウンロードの進行状況を表示することができます。
次のコードは、ossClient.getObject を使用した進行状況を表示する方法を説明するための例として、使用されています。
static class GetObjectProgressListener implements ProgressListener {
private long bytesRead = 0;
private long totalBytes = -1;
private boolean succeed = false;
@Override
public void progressChanged(ProgressEvent progressEvent) {
long bytes = progressEvent.getBytes();
ProgressEventType eventType = progressEvent.getEventType();
switch (eventType) {
case TRANSFER_STARTED_EVENT:
System.out.println("Start to download......");
break;
case RESPONSE_CONTENT_LENGTH_EVENT:
this.totalBytes = bytes;
System.out.println(this.totalBytes + " bytes in total will be downloaded to a local file");
break;
case RESPONSE_BYTE_TRANSFER_EVENT:
this.bytesRead += bytes;
if (this.totalBytes ! = -1) {
int percent = (int)(this.bytesRead * 100.0 / this.totalBytes);
System.out.println(bytes + " bytes have been read at this time, download progress: " +
percent + "%(" + this.bytesRead + "/" + this.totalBytes + ")");
} else {
System.out.println(bytes + " bytes have been read at this time, download ratio: unknown" +
"(" + this.bytesRead + "/...)");
}
break;
case TRANSFER_COMPLETED_EVENT:
this.succeed = true;
System.out.println("Succeed to download, " + this.bytesRead + " bytes have been transferred in total");
break;
case TRANSFER_FAILED_EVENT:
System.out.println("Failed to download, " + this.bytesRead + " bytes have been transferred");
break;
default:
break;
}
}
public boolean isSucceed() {
return succeed;
}
}
public static void main(String[] args) {
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
try {
// Download with a progress bar displayed.
ossClient.getObject(new GetObjectRequest(bucketName, objectName).
<GetObjectRequest>withProgressListener(new GetObjectProgressListener()),
new File("<yourLocalFile>"));
} catch (Exception e) {
e.printStackTrace();
}
// Close your OSSClient instance.
ossClient.shutdown();
}
進行状況は、ossClient.putObject、ossClient.getObject、またはossClient.uploadPart で表示できます。 ただし、ossClient.uploadFile または ossClient.downloadFile を使用して進行状況を表示することはできません。
ダウンロードの進捗プログレスバーの完全なコードについては、『GitHub』をご参照ください。