TyImgUrlConvert.java 3.7 KB

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