import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosException;
import com.volcengine.tos.comm.event.DownloadEventType;
import com.volcengine.tos.model.object.DownloadEvent;
import com.volcengine.tos.model.object.DownloadEventListener;
import com.volcengine.tos.model.object.DownloadFileInput;
import com.volcengine.tos.model.object.DownloadFileOutput;
import com.volcengine.tos.model.object.HeadObjectV2Input;
public class DownloadFileWithEventListenerExample extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
String endpoint = "your endpoint";
String region = "your region";
String accessKey = "your access key";
String secretKey = "your secret key";
String securityToken = "your security token";
String bucketName = "your bucket name";
String objectKey = "your object key";
String downloadFilePath = "the path of file to download";
long partSize = 10 * 1024 * 1024;
boolean enableCheckpoint = true;
String checkpointFilePath = "the checkpoint file path";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey, securityToken);
Thread tosThread = new Thread(new Runnable() {
HeadObjectV2Input head = new HeadObjectV2Input().setBucket(bucketName).setKey(objectKey);
DownloadEventListener listener = new DownloadEventListener() {
public void eventChange(DownloadEvent downloadEvent) {
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventCreateTempFileSucceed) {
Log.i("downloadFile", "event change, createTempFile succeed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventCreateTempFileFailed) {
Log.i("downloadFile", "event change, createTempFile failed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventDownloadPartSucceed) {
Log.i("downloadFile", "event change, downloadPart succeed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventDownloadPartFailed) {
Log.i("downloadFile", "event change, downloadPart failed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventDownloadPartAborted) {
Log.i("downloadFile", "event change, downloadPart aborted");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventRenameTempFileSucceed) {
Log.i("downloadFile", "event change, renameTempFile succeed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventRenameTempFileFailed) {
Log.i("downloadFile", "event change, renameTempFile failed");
DownloadFileInput input = new DownloadFileInput().setHeadObjectV2Input(head)
.setFilePath(downloadFilePath).setEnableCheckpoint(enableCheckpoint)
.setCheckpointFile(checkpointFilePath).setPartSize(partSize).setTaskNum(taskNum)
.setDownloadEventListener(listener);
DownloadFileOutput output = tos.downloadFile(input);
Log.i("downloadFile", "downloadFile succeed, object's etag is " + output.getOutput().getHeadObjectBasicOutput().getEtag());
Log.i("downloadFile", "downloadFile succeed, object's crc64 is " + output.getOutput().getHeadObjectBasicOutput().getHashCrc64ecma());
} catch (TosException e) {
Log.e("TosException","downloadFile failed");