update 优化 oss 上传下载 使用流直接操作 减少读取字节码的内存消耗
This commit is contained in:
parent
e5515751fd
commit
35fac6cc0c
@ -134,10 +134,17 @@ public class OssClient {
|
|||||||
* @param path 完整文件路径
|
* @param path 完整文件路径
|
||||||
*/
|
*/
|
||||||
public ObjectMetadata getObjectMetadata(String path) {
|
public ObjectMetadata getObjectMetadata(String path) {
|
||||||
|
path = path.replace(getUrl() + "/", "");
|
||||||
S3Object object = client.getObject(properties.getBucketName(), path);
|
S3Object object = client.getObject(properties.getBucketName(), path);
|
||||||
return object.getObjectMetadata();
|
return object.getObjectMetadata();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InputStream getObjectContent(String path) {
|
||||||
|
path = path.replace(getUrl() + "/", "");
|
||||||
|
S3Object object = client.getObject(properties.getBucketName(), path);
|
||||||
|
return object.getObjectContent();
|
||||||
|
}
|
||||||
|
|
||||||
public String getUrl() {
|
public String getUrl() {
|
||||||
String domain = properties.getDomain();
|
String domain = properties.getDomain();
|
||||||
String endpoint = properties.getEndpoint();
|
String endpoint = properties.getEndpoint();
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package com.ruoyi.system.service.impl;
|
package com.ruoyi.system.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.http.HttpException;
|
|
||||||
import cn.hutool.http.HttpUtil;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
@ -32,6 +30,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -92,24 +91,21 @@ public class SysOssServiceImpl implements ISysOssService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void download(Long ossId, HttpServletResponse response) throws IOException {
|
public void download(Long ossId, HttpServletResponse response) throws IOException {
|
||||||
SysOssVo sysOss = this.matchingUrl(SpringUtils.getAopProxy(this).getById(ossId));
|
SysOssVo sysOss = this.getById(ossId);
|
||||||
if (ObjectUtil.isNull(sysOss)) {
|
if (ObjectUtil.isNull(sysOss)) {
|
||||||
throw new ServiceException("文件数据不存在!");
|
throw new ServiceException("文件数据不存在!");
|
||||||
}
|
}
|
||||||
FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
|
FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
|
||||||
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
|
||||||
long data;
|
OssClient storage = OssFactory.instance();
|
||||||
try {
|
try(InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) {
|
||||||
data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false);
|
int available = inputStream.available();
|
||||||
} catch (HttpException e) {
|
IoUtil.copy(inputStream, response.getOutputStream(), available);
|
||||||
if (e.getMessage().contains("403")) {
|
response.setContentLength(available);
|
||||||
throw new ServiceException("无读取权限, 请在对应的OSS开启'公有读'权限!");
|
} catch (Exception e) {
|
||||||
} else {
|
|
||||||
throw new ServiceException(e.getMessage());
|
throw new ServiceException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response.setContentLength(Convert.toInt(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SysOssVo upload(MultipartFile file) {
|
public SysOssVo upload(MultipartFile file) {
|
||||||
@ -118,7 +114,7 @@ public class SysOssServiceImpl implements ISysOssService {
|
|||||||
OssClient storage = OssFactory.instance();
|
OssClient storage = OssFactory.instance();
|
||||||
UploadResult uploadResult;
|
UploadResult uploadResult;
|
||||||
try {
|
try {
|
||||||
uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
|
uploadResult = storage.uploadSuffix(file.getInputStream(), suffix, file.getContentType());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ServiceException(e.getMessage());
|
throw new ServiceException(e.getMessage());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user