阿里云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();
}