| | |
| | | import com.aliyun.oss.OSS; |
| | | import com.aliyun.oss.OSSClientBuilder; |
| | | import com.aliyun.oss.model.ObjectMetadata; |
| | | import com.aliyun.oss.model.VoidResult; |
| | | import com.ycl.service.oss.OssService; |
| | | import com.ycl.utils.ConstantPropertiesUtils; |
| | | import com.ycl.utils.common.RandomUtils; |
| | |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.*; |
| | | import java.net.URL; |
| | | import java.net.URLDecoder; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | |
| | | // 上传文件流 |
| | | try { |
| | | // 获取文件的名称 |
| | | String strDate2 = dtf2.format(LocalDateTime.now()); |
| | | |
| | | byte[] newImg = file.getBytes(); |
| | | String originalFilename = file.getOriginalFilename(); |
| | | String fileType = originalFilename.substring(originalFilename.lastIndexOf(".")); |
| | | |
| | | String fileName = "sczf/" + strDate2 + RandomUtils.generateRandomInt(4) + "." + fileType; |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | String fileName = "sczf/" + LocalDateTime.now().format(formatter) + RandomUtils.generateRandomInt(8) + fileType; |
| | | |
| | | if (".png".equalsIgnoreCase(fileType) || ".jpg".equalsIgnoreCase(fileType) || ".jpeg".equalsIgnoreCase(fileType)) { |
| | | if (file.getSize() > 2097152) { |
| | | newImg = compressUnderSize(file.getBytes(), 2097152); |
| | |
| | | // 把上传的文件路径返回 (手动拼接) |
| | | // 这里设置图片有效时间 我设置了30年 |
| | | Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000); |
| | | |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | |
| | | System.out.println("上传结束--" + url + "------" + dtf2.format(LocalDateTime.now())); |
| | |
| | | |
| | | @Override |
| | | public boolean deleteImages(String fileUrl) { |
| | | String endpoint = ConstantPropertiesUtils.END_POINT; |
| | | String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID; |
| | | String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET; |
| | | String bucketName = ConstantPropertiesUtils.BUCKET_NAME; |
| | | //创建OSSClient实例 |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | String imgFile = fileUrl.replace("https://bucket-ans.oss-cn-hangzhou.aliyuncs.com/", ""); |
| | | String fileName = imgFile.substring(0, imgFile.indexOf("?")); |
| | | try { |
| | | |
| | | // 根据BucketName,objectName删除文件 |
| | | ossClient.deleteObject(bucketName, fileName); |
| | | ossClient.shutdown(); |
| | | return true; |
| | | String endpoint = ConstantPropertiesUtils.END_POINT; |
| | | String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID; |
| | | String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET; |
| | | String bucketName = ConstantPropertiesUtils.BUCKET_NAME; |
| | | //创建OSSClient实例 |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | |
| | | String imgFile = fileUrl.replace(ConstantPropertiesUtils.PREFIX_URL, ""); |
| | | String fileName = imgFile; |
| | | if (imgFile.contains("?")) { |
| | | fileName = imgFile.substring(0, imgFile.indexOf("?")); |
| | | } |
| | | try { |
| | | |
| | | fileName = URLDecoder.decode(fileName, "UTF-8"); |
| | | } catch (UnsupportedEncodingException ex) { |
| | | // throw new RuntimeException(ex); |
| | | } |
| | | |
| | | // 根据BucketName,objectName删除文件 |
| | | boolean b = ossClient.doesObjectExist(bucketName, fileName); |
| | | if (b) { |
| | | VoidResult voidResult = ossClient.deleteObject(bucketName, fileName); |
| | | System.out.println(voidResult.toString()); |
| | | } |
| | | ossClient.shutdown(); |
| | | return true; |
| | | |
| | | } catch (Exception e) { |
| | | System.out.println("-------删除图片失败,图片地址:" + fileUrl); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | @Override |