From 350c88fffb2646d04930ca45f8bdcef5ea1e9617 Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期三, 22 一月 2025 15:30:42 +0800 Subject: [PATCH] 房屋 --- ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java | 28 ++++++++++++++++++---------- ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java | 14 +++++++++++--- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java index eeba769..5fe8e8d 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java @@ -13,6 +13,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import java.io.OutputStream; /** * @author HJL @@ -33,8 +34,15 @@ @ApiImplicitParam(value = "文件", name = "file", dataType = "MultipartFile", required = true) }) public R<String> upload(@RequestParam("file") MultipartFile file) { - String s = tencentCosUtil.upLoadFile(file); - return R.ok(s, s); + String url = tencentCosUtil.upLoadFile(file); + return R.ok(url, url); } - + @PostMapping("/download") + @ApiOperation(value = "文件下载", tags = "公共-文件下载") + @ApiImplicitParams({ + @ApiImplicitParam(value = "文件url", name = "url", dataType = "String", required = true) + }) + public void upload(@RequestParam("url") String url) { + tencentCosUtil.downLoadFile(url); + } } 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 af76a4f..fe5f78f 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 @@ -13,10 +13,12 @@ import com.qcloud.cos.model.PutObjectResult; import com.qcloud.cos.region.Region; import com.qcloud.cos.utils.IOUtils; +import com.ruoyi.common.utils.WebUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -117,32 +119,38 @@ return null; } } -// https://xzgttest-1305134071.cos.ap-chengdu.myqcloud.com/xizang/e4ea88b8-5470-456e-bf97-75cf47f38e84.jpg - public static void main(String[] args) throws IOException { - // 1 初始化用户身份信息(secretId, secretKey)。 + + /** + * 下载文件 + * @param file + * @return + */ + public void downLoadFile(String file) { + HttpServletResponse response = WebUtils.response(); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + String replace = file.replace(rootSrc, ""); COSCredentials cred = new BasicCOSCredentials( - "AKIDCF5EF2c0DE1e5JK8r4EGJF4mNsMgp26x", - "lLl184rUyFOOE0d5KNGC3kmfNsCWk4GU"); + secretId, + secretKey); // 2.1 设置存储桶的地域(上文获得) - Region region = new Region("ap-chengdu"); + Region region = new Region(bucketAddr); ClientConfig clientConfig = new ClientConfig(region); // 2.2 使用https协议传输 clientConfig.setHttpProtocol(HttpProtocol.https); COSClient cosClient = new COSClient(cred, clientConfig); - String key = "/xizang/e4ea88b8-5470-456e-bf97-75cf47f38e84.jpg"; try { // 5. 下载文件并获取输入流 - InputStream inputStream = cosClient.getObject("xzgttest-1305134071", key).getObjectContent(); + InputStream inputStream = cosClient.getObject(bucketName, replace).getObjectContent(); // 6. 处理输入流,例如读取内容或保存到本地文件 // 这里仅作示例,实际应用中需要根据需求处理输入流 byte[] buffer = new byte[1024]; int len; - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); while ((len = inputStream.read(buffer)) != -1) { // 处理读取到的数据 System.out.write(buffer, 0, len); } + response.getOutputStream().write(byteArrayOutputStream.toByteArray()); } catch (Exception e) { e.printStackTrace(); } finally { @@ -150,5 +158,5 @@ cosClient.shutdown(); } } - +// https://xzgttest-1305134071.cos.ap-chengdu.myqcloud.com/xizang/e4ea88b8-5470-456e-bf97-75cf47f38e84.jpg } \ No newline at end of file -- Gitblit v1.7.1