视频点播 VOD-生成MD5值:上传校验

时间:2024-06-19 15:57:46

上传校验

调用上传检验接口时,点播服务会根据媒资的MD5值来检查是否已有重复的媒资文件。MD5值的生成方式是取媒资文件的1024字节,并进行MD5计算,示例代码如下所示:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.apache.commons.codec.digest.DigestUtils;

public class VodDemoDuplicateCheckMd5 {
    public static String computeMd5ByFile(String fileUrl) {
        String md5Content = null;
        Path targetFile = Paths.get(fileUrl);
        try (SeekableByteChannel channel = Files.newByteChannel(targetFile, StandardOpenOption.READ)) {
            ByteBuffer byteBuffer = ByteBuffer.allocate(1025);
            channel.read(byteBuffer);
            byteBuffer.flip();
            byte[] data = new byte[byteBuffer.limit()];
            byteBuffer.get(data);
            md5Content = DigestUtils.md5Hex(data);
        } catch (IOException e) {
            throw new RuntimeException(String.format("Read file %s failed.", fileUrl));
        }
        return md5Content;
    }
}
support.huaweicloud.com/api-vod/vod_04_0212.html