在使用阿里云oss的时候,指定content-md5进行文件流校验
那么怎么获取?
File pdfFile = new File("/Users/chongchuanbing/Downloads/19E06F28-3AE3-4E18-919F-AC1DA55FDFE8.png");
FileInputStream fileInputStream = new FileInputStream(pdfFile);
byte[] bytes = IOUtils.toByteArray(fileInputStream);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
MessageDigest md = MessageDigest.getInstance(MessageDigestAlgorithms.MD5);
byte[] b = md.digest(IOUtils.toByteArray(byteArrayInputStream));
String contextMd5 = Base64.encodeBase64String(b);
System.err.println("content-md5 first: " + contextMd5);
System.err.println("content-md5 second: " + BinaryUtil.toBase64String(DigestUtils.md5(bytes)));
System.err.println("content-md5 third: " + BinaryUtil.toBase64String(DigestUtils.md5(new ByteArrayInputStream(bytes))));
MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), new ByteArrayInputStream(bytes));
String contentMd5 = BinaryUtil.toBase64String(DigestUtils.md5(multipartFile.getInputStream()));
System.err.println("content-md5 four: " + contentMd5);
MockMultipartFile.getInputStream 源码如下
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(this.content);
}
结论:
- InputStream 转化为byte之后,内部index发生改变,上传有问题
- MockMultipartFile内部存储的是byte,使用时转成ByteArrayInputStream,使用没有问题
网友评论