Bladeren bron

拓元商品下载转换

谭斌 1 jaar geleden
bovenliggende
commit
aed361db84

+ 16 - 0
device-api-service/src/main/java/com/xy/service/ExportAsyncService.java

@@ -3,6 +3,7 @@ package com.xy.service;
 import com.xy.annotate.RestMappingController;
 import com.xy.dto.DeviceInfoDto;
 import com.xy.utils.R;
+import com.xy.utils.TyImgUrlConvert;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.RequiredArgsConstructor;
@@ -11,6 +12,7 @@ import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 
 import javax.validation.Valid;
+import java.io.IOException;
 
 /***
  * 异步导出处理
@@ -31,4 +33,18 @@ public class ExportAsyncService {
         deviceInfoService.exportDevicesAsync(page);
         return R.ok();
     }
+
+    @ApiOperation("拓元商品上传")
+    @PostMapping("tyGoodsImgUpload")
+    public R tyGoodsImgUpload() {
+        try {
+            TyImgUrlConvert.upload();
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        return R.ok();
+    }
+
 }

+ 17 - 7
device-api-service/src/main/java/com/xy/utils/TyImgUrlConvert.java

@@ -1,5 +1,6 @@
 package com.xy.utils;
 
+import cn.hutool.system.SystemUtil;
 import com.aliyun.oss.ClientConfiguration;
 import com.aliyun.oss.OSS;
 import com.aliyun.oss.OSSClient;
@@ -31,16 +32,26 @@ import java.util.concurrent.Future;
 public class TyImgUrlConvert {
 
     public static void main(String[] args) throws Exception {
+        upload();
+    }
+
+    public static void upload() throws InterruptedException, IOException {
         // 初始化OSS
         OSS ossClient = new OSSClient("oss-cn-beijing.aliyuncs.com",
                 new DefaultCredentialProvider("LTAI5tKyfiX3LT2PENxrax6a", "QDpoLnUek0e0DE613mECgIirDExW89"),
                 new ClientConfiguration());
+
+        String path = "D:\\test.xlsx";
+        if (SystemUtil.getOsInfo().isLinux()) {
+            path = "/home/test.xlsx";
+        }
         // 读取Excel
-        XSSFWorkbook workbook = new XSSFWorkbook("D:\\test.xlsx");
+        XSSFWorkbook workbook = new XSSFWorkbook(path);
+
         XSSFSheet sheet = workbook.getSheetAt(0);
 
         // 创建线程池
-        ExecutorService executor = Executors.newFixedThreadPool(5);
+        ExecutorService executor = Executors.newFixedThreadPool(20);
 
         // 遍历每一行数据
         for (Row row : sheet) {
@@ -68,16 +79,16 @@ public class TyImgUrlConvert {
                     BufferedImage image = ImageIO.read(connection.getInputStream());
 
                     // 上传图片到阿里云对象存储
-                    File tempFile = File.createTempFile("image", ".jpg");
+                    File tempFile = File.createTempFile("image", ".png");
                     FileOutputStream fos = new FileOutputStream(tempFile);
-                    ImageIO.write(image, "jpg", fos);
+                    ImageIO.write(image, "png", fos);
                     fos.close();
-                    String fileUrl = "tyGoods/" + barcode + ".jpg";
+                    String fileUrl = "tyGoods/" + barcode + ".png";
                     PutObjectResult result = ossClient.putObject(bucketName, fileUrl, tempFile);
                     // 设置权限(公开读)
                     ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
                     if (result != null) {
-//                        https://cdn.ossfile.mxrvending.com/tyGoods/084501446314MXR.jpg
+//                        https://cdn.ossfile.mxrvending.com/tyGoods/6948960111210.png
                         System.out.println("------OSS文件上传成功------ " + fileUrl);
                     }
                     tempFile.delete();
@@ -94,7 +105,6 @@ public class TyImgUrlConvert {
                 Thread.sleep(100);
             }
         }
-
         // 关闭线程池
         executor.shutdown();
     }