From 75264cbbd3a0dcf2b068362b0562572876a7cd5c Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期四, 23 一月 2025 11:51:06 +0800 Subject: [PATCH] 房屋合同 --- ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java | 35 ++++++++++++++++++++++++++++++++++- 1 files changed, 34 insertions(+), 1 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java index be2bdbb..1d02134 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Base64; import java.util.UUID; /** @@ -129,6 +130,7 @@ public void downLoadFile(String file) { HttpServletResponse response = WebUtils.response(); String replace = file.replace(rootSrc, ""); + response.setHeader("Access-Control-Expose-Headers","File-Type"); COSCredentials cred = new BasicCOSCredentials( secretId, secretKey); @@ -157,5 +159,36 @@ cosClient.shutdown(); } } -// https://xzgttest-1305134071.cos.ap-chengdu.myqcloud.com/xizang/e4ea88b8-5470-456e-bf97-75cf47f38e84.jpg + public String downLoadFileImg(String file) { + byte[] data = null; + String replace = file.replace(rootSrc, ""); + COSCredentials cred = new BasicCOSCredentials( + secretId, + secretKey); + // 2.1 设置存储桶的地域(上文获得) + Region region = new Region(bucketAddr); + ClientConfig clientConfig = new ClientConfig(region); + // 2.2 使用https协议传输 + clientConfig.setHttpProtocol(HttpProtocol.https); + COSClient cosClient = new COSClient(cred, clientConfig); + try { + // 5. 下载文件并获取输入流 + InputStream inputStream = cosClient.getObject(bucketName, replace).getObjectContent(); + ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); + // 6. 处理输入流,例如读取内容或保存到本地文件 + byte[] buffer = new byte[1024]; + int len; + while ((len = inputStream.read(buffer)) != -1) { + // 处理读取到的数据 + swapStream.write(buffer, 0, len); + } + data = swapStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + // 7. 关闭输入流 + cosClient.shutdown(); + } + return Base64.getEncoder().encodeToString(data); + } } \ No newline at end of file -- Gitblit v1.7.1