123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package com.ygj.yuemum.utils;
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClient;
- import com.aliyun.oss.common.auth.DefaultCredentialProvider;
- import com.aliyun.oss.model.ObjectMetadata;
- import lombok.Data;
- import lombok.experimental.Accessors;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.util.UUID;
- @Service
- public class OssService {
- String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
- String accessKeyId = "02yUhyzfxsxzLCKA";
- String accessKeySecret = "6Cw3uKQKKRedocMtfZVfCxHoeZh2h6";
- String bucket = "yueguanjia-bucket";
- String rootserver = "oss-cn-hangzhou.aliyuncs.com";
- public UploadDto upload(File file, String folder) throws Exception {
- OSS ossClient = new OSSClient(endpoint, new DefaultCredentialProvider(accessKeyId, accessKeySecret), null);
- ObjectMetadata objectMetadata = new ObjectMetadata();
- objectMetadata.setContentLength(file.length());
- FileInputStream inputStream = new FileInputStream(file);
- String extension = getExtension(file.getName());
- String saveName = UUID.randomUUID().toString() + "." + extension;
- String key = folder + "/" + saveName;
- ossClient.putObject(bucket, key, inputStream, objectMetadata);
- ossClient.shutdown();
- UploadDto uploadDto = new UploadDto();
- uploadDto.url = getUrl(key);
- uploadDto.name = file.getName();
- uploadDto.saveName = saveName;
- uploadDto.size = file.length();
- uploadDto.key = key;
- return uploadDto;
- }
- public UploadDto upload(MultipartFile file, String folder) throws Exception {
- OSS ossClient = new OSSClient(endpoint, new DefaultCredentialProvider(accessKeyId, accessKeySecret), null);
- ObjectMetadata objectMetadata = new ObjectMetadata();
- objectMetadata.setContentLength(file.getSize());
- String extension = getExtension(file.getOriginalFilename());
- String saveName = UUID.randomUUID().toString() + "." + extension;
- String key = folder + "/" + saveName;
- ossClient.putObject(bucket, key, file.getInputStream(), objectMetadata);
- ossClient.shutdown();
- UploadDto uploadDto = new UploadDto();
- uploadDto.url = getUrl(key);
- uploadDto.name = file.getOriginalFilename();
- uploadDto.saveName = saveName;
- uploadDto.size = file.getSize();
- uploadDto.key = key;
- return uploadDto;
- }
- public UploadDto upload(InputStream inputStream, String folder, String fileName, long lenth) throws UnsupportedEncodingException {
- OSS ossClient = new OSSClient(endpoint, new DefaultCredentialProvider(accessKeyId, accessKeySecret), null);
- String extension = getExtension(fileName);
- String saveName = UUID.randomUUID().toString() + "." + extension;
- String key = folder + "/" + saveName;
- ossClient.putObject(bucket, key, inputStream);
- ossClient.shutdown();
- UploadDto uploadDto = new UploadDto();
- uploadDto.url = getUrl(key);
- uploadDto.name = fileName;
- uploadDto.saveName = saveName;
- uploadDto.size = lenth;
- uploadDto.key = key;
- return uploadDto;
- }
- private String getExtension(String fileName) {
- int index = fileName.lastIndexOf(".");
- if (index < 0) {
- return "";
- }
- return fileName.substring(index + 1);
- }
- public String getUrl(String key) throws UnsupportedEncodingException {
- return "https://" + bucket + "." + rootserver + "/" + URLEncoder.encode(key, "UTF-8");
- }
- @Data
- @Accessors(chain = true)
- public static class UploadDto {
- /**
- * oss存储地址
- */
- private String url;
- /**
- * osskey
- */
- private String key;
- private String name;
- /**
- * 保存oss文件名称
- */
- private String saveName;
- /**
- * 文件大小
- */
- private Long size;
- }
- }
|