本文主要是介绍阿里云oss Multipart Upload 中每个part的E-tag(即MD5)求法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
阿里云java的开发文档中提到:OSS 会将服务器端收到 Part 数据的 MD5 值放在 ETag 头内返回给用户。 为了保证数据在网络传输过程中不出现错误,强烈推荐用户在收到 OSS 的返回请求后,用该 MD5 值验证上传数据的正确性。
但是没有告诉怎么验证,纠结了一天之后终于找到了在本地求每个part的MD5值得方法:
<span style="font-size:18px;">private static HashMap<Integer, String> FileMD5(File bigFile) throws IOException{int partCount = calPartCount(bigFile);HashMap<Integer, String> eTagMap = new HashMap<Integer, String>();byte[] tempByte= new byte[5*1024*1024];FileInputStream in = new FileInputStream(bigFile);for(int i = 0; i<partCount-1;i++){try {</span>
<span style="font-size:18px;"><span style="white-space:pre"> </span>//part临时存储的路径String outFilePath = "H:/aliyun/方寸指间图书分享"+(i+1);File outPartFile = new File(outFilePath);FileOutputStream out = new FileOutputStream(outPartFile);in.read(tempByte);out.write(tempByte);String md5 =DigestUtils.md5Hex(new FileInputStream(outPartFile));eTagMap.put(i+1, md5);System.out.println("第"+(i+1)+"块数据的MD5:"+md5);out.close();if(outPartFile.exists())outPartFile.delete();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}</span>
<span style="font-size:18px;"><span style="white-space:pre"> </span>//最后一个part的MD5String md5 =DigestUtils.md5Hex(in);eTagMap.put(partCount, md5);System.out.println("第"+partCount+"块数据的MD5:"+md5);return eTagMap;}</span>
通过返回的MAP对象就可以跟oss返回的MD5进行比对来验证每个part是否上传成功。
这篇关于阿里云oss Multipart Upload 中每个part的E-tag(即MD5)求法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!