From 35fac6cc0cd8a1fb944d7696d6f51c0f64df9a35 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期二, 08 十一月 2022 18:47:19 +0800 Subject: [PATCH] update 优化 oss 上传下载 使用流直接操作 减少读取字节码的内存消耗 --- ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java | 26 +++++++++++--------------- 1 files changed, 11 insertions(+), 15 deletions(-) diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java index f3bb136..4b1d254 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java @@ -1,9 +1,7 @@ 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.http.HttpException; -import cn.hutool.http.HttpUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -32,6 +30,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -92,23 +91,20 @@ @Override 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)) { throw new ServiceException("鏂囦欢鏁版嵁涓嶅瓨鍦�!"); } FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName()); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8"); - long data; - try { - data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false); - } catch (HttpException e) { - if (e.getMessage().contains("403")) { - throw new ServiceException("鏃犺鍙栨潈闄�, 璇峰湪瀵瑰簲鐨凮SS寮�鍚�'鍏湁璇�'鏉冮檺!"); - } else { - throw new ServiceException(e.getMessage()); - } + OssClient storage = OssFactory.instance(); + try(InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) { + int available = inputStream.available(); + IoUtil.copy(inputStream, response.getOutputStream(), available); + response.setContentLength(available); + } catch (Exception e) { + throw new ServiceException(e.getMessage()); } - response.setContentLength(Convert.toInt(data)); } @Override @@ -118,7 +114,7 @@ OssClient storage = OssFactory.instance(); UploadResult uploadResult; try { - uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType()); + uploadResult = storage.uploadSuffix(file.getInputStream(), suffix, file.getContentType()); } catch (IOException e) { throw new ServiceException(e.getMessage()); } -- Gitblit v1.9.3