From 620be2be16ea6cf4b6f428f3334fa34035a88736 Mon Sep 17 00:00:00 2001 From: kvtb <76634406+kvtb@users.noreply.github.com> Date: Fri, 30 Sep 2022 05:56:12 +0000 Subject: [PATCH] client-java: provide Content-MD5 when uploading chunks (#3761) * client-java: provide Content-MD5 when uploading chunks * Update SeaweedWrite.java --- .../src/main/java/seaweedfs/client/SeaweedWrite.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/other/java/client/src/main/java/seaweedfs/client/SeaweedWrite.java b/other/java/client/src/main/java/seaweedfs/client/SeaweedWrite.java index df45dd3f2..48256a3e6 100644 --- a/other/java/client/src/main/java/seaweedfs/client/SeaweedWrite.java +++ b/other/java/client/src/main/java/seaweedfs/client/SeaweedWrite.java @@ -14,7 +14,9 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.security.SecureRandom; +import java.security.MessageDigest; import java.util.List; +import java.util.Base64; public class SeaweedWrite { @@ -123,13 +125,20 @@ public class SeaweedWrite { final byte[] bytes, final long bytesOffset, final long bytesLength, byte[] cipherKey) throws IOException { + MessageDigest md = null; + try { + md = MessageDigest.getInstance("MD5"); + } catch (java.security.NoSuchAlgorithmException e) { + } InputStream inputStream = null; if (cipherKey == null || cipherKey.length == 0) { + md.update(bytes, (int) bytesOffset, (int) bytesLength); inputStream = new ByteArrayInputStream(bytes, (int) bytesOffset, (int) bytesLength); } else { try { byte[] encryptedBytes = SeaweedCipher.encrypt(bytes, (int) bytesOffset, (int) bytesLength, cipherKey); + md.update(encryptedBytes); inputStream = new ByteArrayInputStream(encryptedBytes, 0, encryptedBytes.length); } catch (Exception e) { throw new IOException("fail to encrypt data", e); @@ -140,6 +149,7 @@ public class SeaweedWrite { if (auth != null && auth.length() != 0) { post.addHeader("Authorization", "BEARER " + auth); } + post.addHeader("Content-MD5", Base64.getEncoder().encodeToString(md.digest())); post.setEntity(MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)