fix 修复图片预览问题

This commit is contained in:
AprilWind 2024-05-23 09:32:09 +08:00
parent 7367457500
commit 9a15298e63

View File

@ -201,10 +201,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 +220,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 +337,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 +350,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));
} }
/** /**