From 7317ba5d24d6377c496a6be892f9256b527db80f Mon Sep 17 00:00:00 2001 From: "Seth.Tian" Date: Mon, 15 Mar 2021 11:40:32 +0800 Subject: [PATCH] =?UTF-8?q?QFX-5192=20update:=E5=86=99=E5=85=A5=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=9B=9E=E9=80=80,=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E4=BF=AE=E6=94=B9metadata=E7=9A=84length?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/core/S3ResourceRepository.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java b/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java index 49bd4e6..79b60ca 100644 --- a/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java +++ b/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java @@ -28,7 +28,7 @@ import com.fr.workspace.resource.ResourceIOException; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.net.URL; -import java.nio.charset.Charset; +import java.net.URLConnection; import java.util.ArrayList; import java.util.List; @@ -163,7 +163,21 @@ public class S3ResourceRepository extends BaseResourceRepository { @Override public void write(String path, byte[] data) { - s3.putObject(bucket, path, new String(data, Charset.forName(CHAR_SET))); + ObjectMetadata metadata; + try { + metadata = s3.getObjectMetadata(bucket, path); + } catch (Exception e) { + metadata = new ObjectMetadata(); + String mimeType = URLConnection.guessContentTypeFromName(path); + if (mimeType != null) { + metadata.setContentType(mimeType); + } + metadata.setContentLength(data.length); + } + if (metadata != null) { + metadata.setContentLength(data.length); + } + s3.putObject(bucket, path, new ByteArrayInputStream(data), metadata); }