| | |
| | | import com.sandu.common.file.IFileUpload; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | |
| | | * @date 2020/12/17 15:31 |
| | | */ |
| | | @Slf4j |
| | | @AllArgsConstructor |
| | | @Component |
| | | public class AliOssFileServiceImpl implements IFileUpload { |
| | | |
| | | private final String endPoint; |
| | | private final String keyId; |
| | | private final String keySecret; |
| | | private final String bucketName; |
| | | private final String host; |
| | | private final String endPoint="oss-cn-shanghai.aliyuncs.com"; |
| | | private final String keyId="LTAI5tPdpt5wvJyLipRijFSP"; |
| | | private final String keySecret="1ahYfCKd0yTddsUnuDLQzI23MLh4VQ"; |
| | | private final String bucketName= "ximonsmart"; |
| | | private String host; |
| | | |
| | | @Override |
| | | public FileUploadDto uploadFile(MultipartFile multipartFile) { |
| | |
| | | long size = multipartFile.getSize(); |
| | | String suffix = FileUtil.getSuffix(originalFilename); |
| | | String dayStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")); |
| | | String fileName = dayStr + UUID.fastUUID().toString() + "." + suffix; |
| | | String fileName = dayStr + UUID.fastUUID()+ "." + suffix; |
| | | |
| | | FileUploadDto uploadDto = new FileUploadDto(); |
| | | OSS ossClient = new OSSClientBuilder().build(endPoint, keyId, keySecret); |
| | |
| | | uploadDto.setHeight(img.getHeight()); |
| | | } |
| | | } |
| | | |
| | | return uploadDto; |
| | | } |
| | | |
| | | public FileUploadDto uploadFile(File multipartFile) { |
| | | String originalFilename = multipartFile.getName(); |
| | | // long size = multipartFile.get(); |
| | | String suffix = FileUtil.getSuffix(originalFilename); |
| | | String dayStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")); |
| | | String fileName = dayStr + UUID.fastUUID()+ "." + suffix; |
| | | |
| | | FileUploadDto uploadDto = new FileUploadDto(); |
| | | OSS ossClient = new OSSClientBuilder().build(endPoint, keyId, keySecret); |
| | | try { |
| | | //容器不存在,就创建 |
| | | if (!ossClient.doesBucketExist(bucketName)) { |
| | | ossClient.createBucket(bucketName); |
| | | CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName); |
| | | createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead); |
| | | ossClient.createBucket(createBucketRequest); |
| | | } |
| | | //上传文件 |
| | | PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName, fileName, multipartFile)); |
| | | //设置权限 这里是公开读 |
| | | // ossClient.setBucketAcl(ossProperties.getBucketName(), CannedAccessControlList.PublicRead); |
| | | |
| | | if (result != null) { |
| | | String url = ""; |
| | | // https://BucketName.Endpoint/ObjectName |
| | | if (StrUtil.isBlank(host)) { |
| | | url = "https://" + bucketName + "." + endPoint + "/" + fileName; |
| | | } else { |
| | | if (!host.endsWith("/")) { |
| | | url = host + "/" + fileName; |
| | | } else { |
| | | url = host + fileName; |
| | | } |
| | | } |
| | | uploadDto.setFileUrl(url); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | throw new BusinessException(e.getMessage()); |
| | | } finally { |
| | | //关闭 |
| | | ossClient.shutdown(); |
| | | } |
| | | |
| | | uploadDto.setOriginName(originalFilename); |
| | | uploadDto.setFileName(fileName); |
| | | uploadDto.setFileType(FileUtil.extName(originalFilename)); |
| | | uploadDto.setStorageType(FileStorageEnums.ALIBABA.getCode()); |
| | | |
| | | return uploadDto; |
| | | } |
| | | } |