zhangmei
2025-02-28 1fe1ac0e09132e0147e48007986be235e4130aa1
ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java
@@ -13,14 +13,18 @@
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.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.UUID;
/**
@@ -117,31 +121,36 @@
            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();
        String replace = file.replace(rootSrc, "");
        response.setHeader("Access-Control-Expose-Headers","File-Type");
        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();
            ServletOutputStream outputStream = response.getOutputStream();
            // 6. 处理输入流,例如读取内容或保存到本地文件
            // 这里仅作示例,实际应用中需要根据需求处理输入流
            byte[] buffer = new byte[1024];
            int len;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            while ((len = inputStream.read(buffer)) != -1) {
                // 处理读取到的数据
                System.out.write(buffer, 0, len);
                outputStream.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
@@ -150,5 +159,36 @@
            cosClient.shutdown();
        }
    }
    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);
    }
}