欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

七牛---上传crc32校检说明与用法

很多用户不知道怎样使用七牛的crc32校检功能,这篇文章主要介绍下crc32的作用以及使用方法。

首先关于crc32的相关概念可以参考这个文章详细介绍:CRC32

七牛这边上传设置crc32之后,上传前会先计算本地文件的crc32,然后和上传到七牛后文件的crc32进行对比,如果两者不同的话就不会保存并且会返回406:上传的数据 CRC32 校验错误。

我们不同的SDK用法都是类似的,就是在上传的时候设置checkCrc参数为true就可以了,比如Java sdk里面上传的put方法里面将checkCrc设置为true就可以了: 
public Response put(XXXX data, String key, String token, StringMap params, 
String mime, boolean checkCrc) throws QiniuException

具体可以参考七牛这边关于设定crc32之后计算crc32以及参数传递的方法: 
https://github.com/qiniu/java-sdk/blob/43713ccf93cf17fd21ddd919434e1df96d291ead/src/main/java/com/qiniu/storage/FormUploader.java#L78-L99

private void buildParams() throws QiniuException {
        params.put("token", token);
        if (key == null) {
            fileName = "filename";
        } else {
            fileName = key;
            params.put("key", key);
        }
        if (checkCrc) {
            long crc32 = 0;
            if (file != null) {
                try {
                    crc32 = Crc32.file(file);
                } catch (IOException e) {
                    throw new QiniuException(e);
                }
            } else {
                crc32 = Crc32.bytes(data);
            }
            params.put("crc32", "" + crc32);
        }
    }

下面是关于java sdk的简单的demo:

public class UploadWithCrc32 {

    String ACCESS_KEY = "AK";
    String SECRET_KEY = "SK";
    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    UploadManager uploadManager = new UploadManager();

    // 设置指定上传策略
   public String getUpToken(){
        return auth.uploadToken("phpdemo", null, 3600,null);
    }

   public void upload() throws IOException{
       String FilePath = "/Users/dxy/sync/aaa.jpg";
       try {
           //设置验证crc32
            Response res = uploadManager.put(FilePath, null, getUpToken(), null, null, true);
            System.out.println(res.bodyString()); 
       } catch (QiniuException e) {
           Response r = e.response;
           // 请求失败时简单状态信息
           System.out.println(r.toString());           
       }
   }

    public static void main(String args[]) throws IOException{
//      //可以根据七牛的计算crc32算法打印出crc32的值
//      File f=new File("/Users/dxy/sync/aaa.jpg");
//      System.out.println(Crc32.file(f));

        new UploadWithCrc32().upload();

    }

来自  http://blog.csdn.net/netdxy/article/details/49107585

普通分类: