1. 程式人生 > >百度BOS下載檔案

百度BOS下載檔案

package com.example.mybostest;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.BosObject;
import com.baidubce.services.bos.model.BosObjectSummary;
import com.baidubce.services.bos.model.BucketSummary;
import com.baidubce.services.bos.model.ListObjectsResponse;
import com.baidubce.services.bos.model.ObjectMetadata;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private static final int REQUESTCODE = 101;
private String BuketName = “XXX”;//你的BuketName
private Button MyBtn;
private LinkedList ObjectKeyList = new LinkedList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BosClientConfiguration config = new BosClientConfiguration();
    config.setCredentials(new DefaultBceCredentials(您的AK, 您的SK));   //您的AK/SK
    config.setEndpoint("gz.bcebos.com");    //傳入Bucket所在區域域名
    final BosClient client = new BosClient(config);    //建立BOSClient例項
    MyBtn = findViewById(R.id.MyBtn);

    MyBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    test();
                    ListObjectsResponse listing = client.listObjects(BuketName);
                    // 遍歷所有Object
                    for (BosObjectSummary objectSummary : listing.getContents()) {
                        ObjectKeyList.add(objectSummary.getKey());
                    }
                    // 獲取Object,返回結果為BosObject物件
                    BosObject object = client.getObject(BuketName, (String) ObjectKeyList.get(0));
                    // 獲取ObjectMeta
                    ObjectMetadata meta = object.getObjectMetadata();

                    // 獲取Object的輸入流
                    InputStream objectContent = object.getObjectContent();

                    // 處理Object

                    try {
                        FileOutputStream fos=new FileOutputStream(Environment.getExternalStorageDirectory()+"/WzBosTest/123.png");//下載檔案的目錄/檔名
                        byte[] buffer=new byte[2048];
                        int count=0;
                        while ((count = objectContent.read(buffer)) >= 0) {
                                fos.write(buffer,0,count);
                        }
                        // 關閉流
                        objectContent.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    });

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUESTCODE) {
        if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[0]
                == PackageManager.PERMISSION_GRANTED) {
            //使用者同意
            test();
        } else {
            //使用者不同意
        }
    }
}
private void test() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int checkSelfPermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (checkSelfPermission == PackageManager.PERMISSION_DENIED) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUESTCODE);
        }
    }
    File appDir = new File(Environment.getExternalStorageDirectory()  + "/WzBosTest");
    if (!appDir.exists()) {
        boolean isSuccess = appDir.mkdirs();
        Log.d("isSuccess:" ,"----------0------------------"+isSuccess);
    }
}

}