首先关于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 {
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{
new UploadWithCrc32().upload();
}
来自 http://blog.csdn.net/netdxy/article/details/49107585