fix 修复文件上传图片预览问题

2.X
AprilWind 8 months ago
parent d7ee4f589c
commit d9d9765a4a

@ -165,10 +165,11 @@ public class OssClient {
* @param filePath * @param filePath
* @param key Amazon S3 * @param key Amazon S3
* @param md5Digest MD5 * @param md5Digest MD5
* @param contentType
* @return UploadResult * @return UploadResult
* @throws OssException * @throws OssException
*/ */
public UploadResult upload(Path filePath, String key, String md5Digest) { public UploadResult upload(Path filePath, String key, String md5Digest, String contentType) {
try { try {
// 构建上传请求对象 // 构建上传请求对象
FileUpload fileUpload = transferManager.uploadFile( FileUpload fileUpload = transferManager.uploadFile(
@ -176,6 +177,7 @@ public class OssClient {
y -> y.bucket(properties.getBucketName()) y -> y.bucket(properties.getBucketName())
.key(key) .key(key)
.contentMD5(StringUtils.isNotEmpty(md5Digest) ? md5Digest : null) .contentMD5(StringUtils.isNotEmpty(md5Digest) ? md5Digest : null)
.contentType(contentType)
.build()) .build())
.addTransferListener(LoggingTransferListener.create()) .addTransferListener(LoggingTransferListener.create())
.source(filePath).build()); .source(filePath).build());
@ -201,10 +203,11 @@ public class OssClient {
* @param inputStream * @param inputStream
* @param key Amazon S3 * @param key Amazon S3
* @param length * @param length
* @param contentType
* @return UploadResult * @return UploadResult
* @throws OssException * @throws OssException
*/ */
public UploadResult upload(InputStream inputStream, String key, Long length) { public UploadResult upload(InputStream inputStream, String key, Long length, String contentType) {
// 如果输入流不是 ByteArrayInputStream则将其读取为字节数组再创建 ByteArrayInputStream // 如果输入流不是 ByteArrayInputStream则将其读取为字节数组再创建 ByteArrayInputStream
if (!(inputStream instanceof ByteArrayInputStream)) { if (!(inputStream instanceof ByteArrayInputStream)) {
inputStream = new ByteArrayInputStream(IoUtil.readBytes(inputStream)); inputStream = new ByteArrayInputStream(IoUtil.readBytes(inputStream));
@ -219,6 +222,7 @@ public class OssClient {
.putObjectRequest( .putObjectRequest(
y -> y.bucket(properties.getBucketName()) y -> y.bucket(properties.getBucketName())
.key(key) .key(key)
.contentType(contentType)
.build()) .build())
.build()); .build());
@ -335,7 +339,7 @@ public class OssClient {
* @throws OssException * @throws OssException
*/ */
public UploadResult uploadSuffix(byte[] data, String suffix) { public UploadResult uploadSuffix(byte[] data, String suffix) {
return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length)); return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length), FileUtils.getMimeType(suffix));
} }
/** /**
@ -348,7 +352,7 @@ public class OssClient {
* @throws OssException * @throws OssException
*/ */
public UploadResult uploadSuffix(InputStream inputStream, String suffix, Long length) { public UploadResult uploadSuffix(InputStream inputStream, String suffix, Long length) {
return upload(inputStream, getPath(properties.getPrefix(), suffix), length); return upload(inputStream, getPath(properties.getPrefix(), suffix), length, FileUtils.getMimeType(suffix));
} }
/** /**
@ -360,7 +364,7 @@ public class OssClient {
* @throws OssException * @throws OssException
*/ */
public UploadResult uploadSuffix(File file, String suffix) { public UploadResult uploadSuffix(File file, String suffix) {
return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null); return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null, FileUtils.getMimeType(suffix));
} }
/** /**

Loading…
Cancel
Save