|
@@ -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();
|
|
|
}
|