| | |
| | | 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; |
| | |
| | | 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服务类 |
| | |
| | | |
| | | 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; |
| | | |
| | |
| | | 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; |
| | |
| | | // .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); |
| | |
| | | } |
| | | |
| | | 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(); |
| | | // } |
| | | } |
| | | |
| | | } |