七牛云储存

Author Avatar
人不如故 6月 14, 2017
  • 在其它设备中阅读本文章
  • 初始化
    一般情况下直接使用默认设置,不用单独配置。 可以配置超时时长、分片上传阀值等。

    Configuration config = new Configuration.Builder()

                    .chunkSize(256 * 1024)  //分片上传时,每片的大小。 默认 256K
                    .putThreshhold(512 * 1024)  // 启用分片上传阀值。默认 512K
                    .connectTimeout(10) // 链接超时。默认 10秒
                    .responseTimeout(60) // 服务器响应超时。默认 60秒
                    .recorder(recorder)  // recorder 分片上传时,已上传片记录器。默认 null
                    .recorder(recorder, keyGen)  // keyGen 分片上传时,生成标识符,用于片记录器区分是那个文件的上传记录
                    .zone(Zone.zone0) // 设置区域,指定不同区域的上传域名、备用域名、备用IP。默认 Zone.zone0
                    .build()
    

    // 重用 uploadManager。一般地,只需要创建一个 uploadManager 对象
    UploadManager uploadManager = new UploadManager(config)

  • 上传文件
    七牛上传文件主要用到两个方法:
    一、 UploadManager.put(data, key, token, handler, options);

    /**

    • UploadManager.put参数说明:
      @data byte[]/String/File 数据,可以是byte数组,文件路径,文件。
      @key String 保存在服务器上的资源唯一标识。请参考键值对。 @token String 服务器分配的token。
      @completionHandler UpCompletionHandler 上传回调函数,必填。 @options UploadOptions 如果需要进度通知、crc校验、中途取消、指定mimeType则需要填写相应字段,详见UploadOptions参数说明。

       */
      

      二、 UploadOptions(params, mimeType, checkCrc, progressHandler, cancellationSignal);

    /**
     * UploadOptions参数说明:
     * 
     *参数                   类型                   说明
     *params               Map<String, String>  自定义变量,key必须以 x: 开始。
     *mimeType             String               指定文件的mimeType。
     *checkCrc             boolean              是否验证上传文件。
     *progressHandler      UpProgressHandler    上传进度回调。
     *cancellationSignal   UpCancellationSignal 取消上传,当isCancelled()返回true时,不再执行更多上传。
     */

现在就比较清楚了

下面主要是各种上传姿势了

  • 简单上传
UploadManager uploadManager = new UploadManager();
data = <File对象、或 文件路径、或 字节数组>
String key = <指定七牛服务上的文件名,或 null>;
String token = <从服务端SDK获取>;
uploadManager.put(data, key, token,
 /**
  * UpCompletionHandler参数说明
  * 
  *@key        即uploadManager.put(file, key, ...)方法指定的key。
  *@info       http请求的状态信息等,可记入日志。isOK()返回 true表示上传成功。
  *@response   七牛反馈的信息。可从中解析保存在七牛服务的key等信息,具体字段取决于上传策略的设置。
  */
new UpCompletionHandler() {
    @Override
    public void complete(String key, ResponseInfo info, JSONObject res) {

        Log.i("qiniu", key + ",\r\n " + info + ",\r\n " + res);
    }
}, null);
  • 记录上传进度
    private void uploadProgress() {

        uploadManager.put(data, key, token, handler,
                new UploadOptions(null, null, false,
                        new UpProgressHandler() {
                            /**
                             * @param key      即uploadManager.put(file, key, ...)方法指定的key
                             * @param percent  进度
                             */
                            public void progress(String key, double percent) {
                                Log.i("qiniu", key + ": " + percent);
                            }
                        }, null));

    }
  • 取消上传
    内部代码会检测UpCancellationSignal##isCancelled()的返回值,当其返回true时,将停止上传。 可外部维护一个变量isCancelled,当点击取消按钮时,设置isCancelled = true;。如:
private volatile boolean isCancelled = false;

uploadManager.put(data, key, token,handler,
    new UploadOptions(null, null, false, progressHandler,
        new UpCancellationSignal(){
            public boolean isCancelled(){
                return isCancelled;
            }
        }));



private void cancell() {
    isCancelled = true;
}
  • 记录断点

分片上传中,可将各个已上传的块记录下来,再次上传时,已上传的部分不用再次上传。 断点记录类需实现 com.qiniu.android.storage.Recorder 接口。已提供保存到文件的 FileRecorder 实现。

String dirPath = <断点记录文件保存的文件夹位置>
Recorder recorder = new FileRecorder(dirPath);




KeyGenerator keyGen = new KeyGenerator(){
    public String gen(String key, File file){


        return key + "_._" + new StringBuffer(file.getAbsolutePath()).reverse();
    }
};





Configuration config = new Configuration.Builder()


                    .recorder(recorder, keyGen)  
                    .build();

UploadManager uploadManager = new UploadManager(config);

uploadManager.put(data, key, ...)

初始化设置和上传文件的基本用法就记录到这啦!

坚持原创技术分享,您的支持将鼓励我继续创作!