android OSS 返回结果分析

阿里云OSS 没有的提供android的SDK,但有第三方的。下载地址:http://bbs.aliyun.com/read.php?spm=5176.383663.5.38.NAbIv2&tid=129380

 

在所有的第三方或者阿里提供的SDK中,有这样的一条说明:put object 请求处理成功后,OSS会将收到的文件的MD5值放返回给用户的请求头"ETag"中,以方便用户检查OSS上的数据和要上传的数据内容一致.

是不是可以这样理解呢——返回的MD5值与上传文件的MD5值一致便是上传成功了呢?

 

经过测试发现,这个假设是对的!

 

下面是android SDK的测试过程:

String path = Environment.getExternalStorageDirectory()
    .getAbsolutePath() + "/1378181013353.m4a";
  Log.e("XMPP", "path:" + path);
  File file = new File(path);
  if (file.exists()) {
   Log.e("X", "exists");

   try {
    FileInputStream in = new FileInputStream(file);
    FileChannel ch = in.getChannel();
    MappedByteBuffer byteBuffer = ch.map(
      FileChannel.MapMode.READ_ONLY, 0, file.length());
    
    Log.e("X", "---->"+getMD5String(byteBuffer));
    
    
   } catch (Exception e) {
    e.printStackTrace();
   }

  } else {
   Log.e("X", "not exists");
  }

 

 

public static String getMD5String(MappedByteBuffer bytes) {
  try {
   MessageDigest algorithm = MessageDigest.getInstance("MD5");
   algorithm.reset();
   algorithm.update(bytes);

   return toHexString(algorithm.digest());
  } catch (NoSuchAlgorithmException e) {
   return null;
  }
 }

 private static String toHexString(byte[] bytes) {
  StringBuilder hexString = new StringBuilder();
  for (byte b : bytes) {
   if (Integer.toHexString(0xFF & b).length() == 1) {
    hexString.append("0").append(Integer.toHexString(0xFF & b));
   } else {
    hexString.append(Integer.toHexString(0xFF & b));
   }
  }
  return hexString.toString().toUpperCase();
 }

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩小浪~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值