| | |
| | | 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())); |
| | | System.out.println("oss uploadImages_1上传成功:" + url); |
| | | return url; |
| | | } catch (Exception e) { |
| | | System.out.println("uploadImages上传图片失败:"); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public boolean deleteImages(String fileUrl) throws UnsupportedEncodingException { |
| | | 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); |
| | | public boolean deleteImages(String fileUrl) { |
| | | try { |
| | | |
| | | String imgFile = fileUrl.replace(ConstantPropertiesUtils.PREFIX_URL, ""); |
| | | String fileName = imgFile; |
| | | if (imgFile.contains("?")) { |
| | | fileName = imgFile.substring(0, imgFile.indexOf("?")); |
| | | } |
| | | fileName = URLDecoder.decode(fileName, "UTF-8"); |
| | | 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); |
| | | |
| | | // 根据BucketName,objectName删除文件 |
| | | boolean b = ossClient.doesObjectExist(bucketName, fileName); |
| | | if (b) { |
| | | VoidResult voidResult = ossClient.deleteObject(bucketName, fileName); |
| | | System.out.println(voidResult.toString()); |
| | | String imgFile = fileUrl.replace(ConstantPropertiesUtils.PREFIX_URL, ""); |
| | | String fileName = imgFile; |
| | | if (imgFile.contains("?")) { |
| | | fileName = imgFile.substring(0, imgFile.indexOf("?")); |
| | | } |
| | | |
| | | fileName = URLDecoder.decode(fileName, "UTF-8"); |
| | | |
| | | // 根据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); |
| | | } |
| | | ossClient.shutdown(); |
| | | return true; |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | |
| | | // 获取文件的名称 |
| | | LocalDate date = LocalDate.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | String fileName = "sczf/" + date.format(formatter) + RandomUtils.generateRandomInt(8) + ext; |
| | | String fileName = "sczf/" + date.format(formatter) + RandomUtils.generateRandomInt(8) + "." + ext; |
| | | |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | objectMetadata.setContentType(getcontentType(ext)); |
| | |
| | | Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000); |
| | | |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | // System.out.println("oss上传成功:" + url); |
| | | System.out.println("oss uploadImages_3上传成功:" + url); |
| | | |
| | | // System.out.println("上传完成--" + dtf2.format(LocalDateTime.now())); |
| | | return url; |