TyImgUrlConvert.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.xy.utils;
  2. import com.aliyun.oss.ClientConfiguration;
  3. import com.aliyun.oss.OSS;
  4. import com.aliyun.oss.OSSClient;
  5. import com.aliyun.oss.common.auth.DefaultCredentialProvider;
  6. import com.aliyun.oss.model.CannedAccessControlList;
  7. import com.aliyun.oss.model.PutObjectResult;
  8. import org.apache.poi.ss.usermodel.Cell;
  9. import org.apache.poi.ss.usermodel.Row;
  10. import org.apache.poi.xssf.usermodel.XSSFSheet;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12. import javax.imageio.ImageIO;
  13. import java.awt.image.BufferedImage;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.net.HttpURLConnection;
  19. import java.net.URL;
  20. import java.util.concurrent.ExecutorService;
  21. import java.util.concurrent.Executors;
  22. import java.util.concurrent.Future;
  23. /**
  24. * 拓元算法有效期商品链接下载并转换到喵星人自己的oss上。由于拓元的barcode唯一使用barcode命名图片
  25. *
  26. * @author 谭斌
  27. * @date 2023/11/13
  28. */
  29. public class TyImgUrlConvert {
  30. public static void main(String[] args) throws Exception {
  31. upload();
  32. }
  33. public static void upload() throws InterruptedException, IOException {
  34. // 初始化OSS
  35. OSS ossClient = new OSSClient("oss-cn-beijing.aliyuncs.com",
  36. new DefaultCredentialProvider("LTAI5tKyfiX3LT2PENxrax6a", "QDpoLnUek0e0DE613mECgIirDExW89"),
  37. new ClientConfiguration());
  38. InputStream inputStream = IoUtils.inputStream("tyGoods.xlsx").get();
  39. // 读取Excel
  40. XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
  41. XSSFSheet sheet = workbook.getSheetAt(0);
  42. // 创建线程池
  43. ExecutorService executor = Executors.newFixedThreadPool(20);
  44. // 遍历每一行数据
  45. for (Row row : sheet) {
  46. if (row.getRowNum() == 0) { // 跳过表头
  47. continue;
  48. }
  49. Cell barcodeCell = row.getCell(0);
  50. Cell urlCell = row.getCell(1);
  51. if (barcodeCell == null || urlCell == null) {
  52. continue;
  53. }
  54. String barcode = barcodeCell.getStringCellValue();
  55. String url = urlCell.getStringCellValue();
  56. String bucketName = "mxrnetfiles";
  57. // 下载并上传图片到阿里云对象存储
  58. Future<String> future = executor.submit(() -> {
  59. try {
  60. // 下载图片
  61. HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
  62. connection.setRequestMethod("GET");
  63. connection.connect();
  64. BufferedImage image = ImageIO.read(connection.getInputStream());
  65. // 上传图片到阿里云对象存储
  66. File tempFile = File.createTempFile("image", ".png");
  67. FileOutputStream fos = new FileOutputStream(tempFile);
  68. ImageIO.write(image, "png", fos);
  69. fos.close();
  70. String fileUrl = "tyGoods/" + barcode + ".png";
  71. PutObjectResult result = ossClient.putObject(bucketName, fileUrl, tempFile);
  72. // 设置权限(公开读)
  73. ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
  74. if (result != null) {
  75. // https://cdn.ossfile.mxrvending.com/tyGoods/6948960111210.png
  76. System.out.println("------OSS文件上传成功------ " + fileUrl);
  77. }
  78. tempFile.delete();
  79. return "OK";
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. return "ERROR";
  83. }
  84. });
  85. // 等待所有任务完成
  86. while (!future.isDone()) {
  87. Thread.sleep(100);
  88. }
  89. }
  90. // 关闭线程池
  91. executor.shutdown();
  92. }
  93. }