| | |
| | | // 上传文件流 |
| | | try { |
| | | // 获取文件的名称 |
| | | String fileName = "sczf/" + file.getOriginalFilename(); |
| | | 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; |
| | | if (".png".equalsIgnoreCase(fileType) || ".jpg".equalsIgnoreCase(fileType) || ".jpeg".equalsIgnoreCase(fileType)) { |
| | | if (file.getSize() > 2097152) { |
| | | newImg = compressUnderSize(file.getBytes(), 2097152); |
| | |
| | | Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000); |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | |
| | | System.out.println("上传结束--" + dtf2.format(LocalDateTime.now())); |
| | | System.out.println("上传结束--" + url + "------" + dtf2.format(LocalDateTime.now())); |
| | | return url; |
| | | } catch (Exception e) { |
| | | System.out.println("uploadImages上传图片失败:"); |
| | |
| | | |
| | | DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | System.out.println("准备上传--" + dtf2.format(LocalDateTime.now())); |
| | | // System.out.println("准备上传--" + dtf2.format(LocalDateTime.now())); |
| | | // 创建OSSClient实例。 |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // 上传文件流 |
| | |
| | | // 获取文件的名称 |
| | | 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)); |
| | | |
| | | System.out.println("开始上传--" + dtf2.format(LocalDateTime.now())); |
| | | // System.out.println("开始上传--" + dtf2.format(LocalDateTime.now())); |
| | | |
| | | ossClient.putObject(bucketName, fileName, inputStream, objectMetadata); |
| | | ossClient.shutdown(); |
| | |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | // System.out.println("oss上传成功:" + url); |
| | | |
| | | System.out.println("上传完成--" + dtf2.format(LocalDateTime.now())); |
| | | // System.out.println("上传完成--" + dtf2.format(LocalDateTime.now())); |
| | | return url; |
| | | } catch (Exception e) { |
| | | System.out.println("oss异常:" + e.getMessage()); |