| | |
| | | } |
| | | |
| | | @Override |
| | | public String uploadImages(InputStream inputStream, String ext) { |
| | | String endpoint = ConstantPropertiesUtils.END_POINT; |
| | | String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID; |
| | | String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET; |
| | | String bucketName = ConstantPropertiesUtils.BUCKET_NAME; |
| | | public String uploadImages(InputStream inputStream, String ext, Integer times) { |
| | | if (times < 3) { |
| | | String endpoint = ConstantPropertiesUtils.END_POINT; |
| | | String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID; |
| | | String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET; |
| | | String bucketName = ConstantPropertiesUtils.BUCKET_NAME; |
| | | |
| | | DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | // System.out.println("准备上传--" + dtf2.format(LocalDateTime.now())); |
| | | // 创建OSSClient实例。 |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // 上传文件流 |
| | | try { |
| | | // 获取文件的名称 |
| | | LocalDate date = LocalDate.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | String fileName = "sczf/" + date.format(formatter) + RandomUtils.generateRandomInt(8) + ext; |
| | | // 创建OSSClient实例。 |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // 上传文件流 |
| | | try { |
| | | // 获取文件的名称 |
| | | LocalDate date = LocalDate.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | String fileName = "sczf/" + date.format(formatter) + RandomUtils.generateRandomInt(8) + ext; |
| | | |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | objectMetadata.setContentType(getcontentType(ext)); |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | objectMetadata.setContentType(getcontentType(ext)); |
| | | |
| | | // System.out.println("开始上传--" + dtf2.format(LocalDateTime.now())); |
| | | |
| | | ossClient.putObject(bucketName, fileName, inputStream, objectMetadata); |
| | | ossClient.shutdown(); |
| | | ossClient.putObject(bucketName, fileName, inputStream, objectMetadata); |
| | | ossClient.shutdown(); |
| | | |
| | | Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000); |
| | | Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000); |
| | | |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | // System.out.println("oss上传成功:" + url); |
| | | |
| | | // System.out.println("上传完成--" + dtf2.format(LocalDateTime.now())); |
| | | return url; |
| | | } catch (Exception e) { |
| | | System.out.println("oss异常:" + e.getMessage()); |
| | | // e.printStackTrace(); |
| | | return this.uploadImages(inputStream, ext); |
| | | return url; |
| | | |
| | | } catch (Exception e) { |
| | | System.out.println("oss异常:" + e.getMessage()); |
| | | // e.printStackTrace(); |
| | | return this.uploadImages(inputStream, ext, times + 1); |
| | | } |
| | | } else { |
| | | return ""; |
| | | } |
| | | } |
| | | |