| | |
| | | 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 com.core.common.SnowflakeIdWorker; |
| | | import com.core.exception.CoolException; |
| | | import lombok.Data; |
| | |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.InputStream; |
| | | 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://oss-cn-hangzhou.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; |
| | |
| | | * 上传OSS文件 |
| | | * @return the url |
| | | */ |
| | | @Deprecated |
| | | public String upload(InputStream inputStream, String suffix){ |
| | | if(suffix.trim().equals("file")){ |
| | | suffix="jpg"; |
| | |
| | | } |
| | | throw new CoolException("500-上传失败"); |
| | | } |
| | | public String upload(InputStream inputStream, String suffix,String contentType){ |
| | | if(suffix.trim().equals("file")){ |
| | | suffix="jpg"; |
| | | } |
| | | |
| | | @Deprecated |
| | | public String upload(InputStream inputStream, String filename, String contentType) { |
| | | OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); |
| | | String name = ""; |
| | | try { |
| | | if (!ossClient.doesBucketExist(bucket)) { |
| | | ossClient.createBucket(bucket); |
| | | log.info("您的Bucket不存在,创建Bucket:{}",bucket); |
| | | log.info("您的Bucket不存在,创建Bucket:{}", bucket); |
| | | } |
| | | name = String.valueOf(snowflakeIdWorker.nextId()).concat(".").concat(suffix); |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss"); |
| | | name = format.format(new Date()) + "_" + filename; |
| | | // 添加 ContentType |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | // objectMetadata.setContentType("image/jpg"); |
| | |
| | | URL url = ossClient.generatePresignedUrl(bucket, name, expiration); |
| | | log.info("OSS文件上传成功: {}", name); |
| | | // return String.valueOf(url); |
| | | return "http://zhongyang-ftpserver.oss-cn-hangzhou.aliyuncs.com/"+name; |
| | | } catch (Exception e){ |
| | | return "http://zhongyang-ftpserver.oss-cn-hangzhou.aliyuncs.com/" + name; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | ossClient.shutdown(); |
| | |
| | | throw new CoolException("500-上传失败"); |
| | | } |
| | | |
| | | @Deprecated |
| | | public String upload(InputStream inputStream) { |
| | | return upload(inputStream, ".jpg"); |
| | | } |
| | | |
| | | /** |
| | | * 下载OSS文件 |
| | | * 文件上传 |
| | | * @param filePath OSS路径 |
| | | * @param file 文件File |
| | | */ |
| | | // public void download(String name) { |
| | | public boolean uploadFile(String filePath, File file) { |
| | | OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); |
| | | // 创建PutObjectRequest对象。 |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, filePath, file); |
| | | // 上传文件。 |
| | | PutObjectResult result = ossClient.putObject(putObjectRequest); |
| | | return true; |
| | | } |
| | | |
| | | // /** |
| | | // * 下载文件 |
| | | // * @param filePath OSS路径 |
| | | // */ |
| | | // public ResponseEntity<InputStreamResource> downloadFile(String filePath) throws IOException { |
| | | // OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); |
| | | // try { |
| | | // OSSObject ossObject = ossClient.getObject(bucket, name); |
| | | // InputStream inputStream = ossObject.getObjectContent(); |
| | | // // do transfer |
| | | // inputStream.close(); |
| | | // log.info("OSS文件下载成功: {}", name); |
| | | // } catch (Exception e){ |
| | | // e.printStackTrace(); |
| | | // } finally { |
| | | // ossClient.shutdown(); |
| | | // } |
| | | // OSSObject ossObject = ossClient.getObject(bucket, filePath); |
| | | // InputStream inputStream = ossObject.getObjectContent(); |
| | | // |
| | | // // 创建响应实体,将输出流作为文件内容返回 |
| | | // HttpHeaders headers = new HttpHeaders(); |
| | | // headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); |
| | | // headers.setContentDispositionFormData("attachment", "output.docx"); |
| | | // InputStreamResource resource = new InputStreamResource(convertToByteArrayInputStream(inputStream)); |
| | | // return ResponseEntity.ok() |
| | | // .headers(headers) |
| | | // .body(resource); |
| | | // } |
| | | |
| | | @Deprecated |
| | | public String download(String name) { |
| | | OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); |
| | | try { |
| | | OSSObject ossObject = ossClient.getObject(bucket, name); |
| | | InputStream inputStream = ossObject.getObjectContent(); |
| | | |
| | | // 获取默认下载目录 |
| | | String defaultDownloadDir = System.getProperty("user.home") + "/Downloads"; |
| | | |
| | | // 创建文件保存路径 |
| | | Path filePath = Paths.get(defaultDownloadDir, name); |
| | | |
| | | // 将输入流复制到文件 |
| | | Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING); |
| | | |
| | | // 关闭输入流 |
| | | inputStream.close(); |
| | | |
| | | log.info("OSS文件下载成功,保存路径: {}", filePath); |
| | | return ""+filePath; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | ossClient.shutdown(); |
| | | } |
| | | return ""; |
| | | 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); |
| | | return url.toString(); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | 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(); |
| | | // } |
| | | } |
| | | |
| | | } |