中扬CRM客户关系管理系统
#
LSH
2023-12-01 09ee5added9d59e90310a2586e846137ea597b19
src/main/java/com/zy/crm/common/service/OssService.java
@@ -1,7 +1,6 @@
package com.zy.crm.common.service;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
@@ -12,20 +11,12 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.io.*;
import java.net.URL;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.nio.file.StandardCopyOption;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
 * 阿里云OSS服务类
@@ -37,8 +28,6 @@
    private static Logger log = LoggerFactory.getLogger(OssService.class);
    private static final String OSS_ENDPOINT = "http://zhongyang-ftpserver.oss-cn-hangzhou-internal.aliyuncs.com";
    @Value("${aliyun.oss.id}")
    private String accessKeyId;
@@ -49,7 +38,10 @@
    private String bucket;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    private String OSS_ENDPOINT;
    @Value("${aliyun.oss.endpoint2}")
    private String OSS_ENDPOINT2;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
@@ -155,20 +147,9 @@
//                .body(resource);
//    }
    public static ByteArrayInputStream convertToByteArrayInputStream(InputStream inputStream) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        byte[] data = outputStream.toByteArray();
        return new ByteArrayInputStream(data);
    }
    @Deprecated
    public String download(String name) {
        OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret);
        OSSClient ossClient = new OSSClient(OSS_ENDPOINT2, accessKeyId, accessKeySecret);
        //获取临时下载链接
        URL url = ossClient.generatePresignedUrl(bucket, name, new Date(new Date().getTime() + 3600 * 1000));
        log.info("OSS文件临时下载路径,URL路径: {}", url);
@@ -192,20 +173,20 @@
    }
    public static void main(String[] args) {
        File file = new File("E:\\tmp\\v2-fbbb97b977b5cebc66dc3cefab0ac981_r.jpg");
        try {
            InputStream in = new FileInputStream(file);
            OssService ossService = new OssService();
            ossService.setAccessKeyId("LTAI4GDzr6ioSHuRw2mk22ug");
            ossService.setAccessKeySecret("84CHL7tF21LbU1qpaP0jn9mIAZP9bv");
            ossService.setBucket("tjdt");
            ossService.setEndpoint("http://oss-cn-hangzhou.aliyuncs.com");
            ossService.setSnowflakeIdWorker(new SnowflakeIdWorker());
            System.out.println(ossService.upload(in));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
//        File file = new File("E:\\tmp\\v2-fbbb97b977b5cebc66dc3cefab0ac981_r.jpg");
//        try {
//            InputStream in = new FileInputStream(file);
//            OssService ossService = new OssService();
//            ossService.setAccessKeyId("LTAI4GDzr6ioSHuRw2mk22ug");
//            ossService.setAccessKeySecret("84CHL7tF21LbU1qpaP0jn9mIAZP9bv");
//            ossService.setBucket("tjdt");
//            ossService.setEndpoint("http://oss-cn-hangzhou.aliyuncs.com");
//            ossService.setSnowflakeIdWorker(new SnowflakeIdWorker());
//            System.out.println(ossService.upload(in));
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
    }
}