|
@@ -0,0 +1,79 @@
|
|
|
|
+package cn.com.qmth.examcloud.tool;
|
|
|
|
+
|
|
|
|
+import ch.qos.logback.classic.LoggerContext;
|
|
|
|
+import cn.com.qmth.examcloud.tool.utils.oss.OssConfig;
|
|
|
|
+import cn.com.qmth.examcloud.tool.utils.oss.OssFileInfo;
|
|
|
|
+import cn.com.qmth.examcloud.tool.utils.oss.OssHelper;
|
|
|
|
+import org.junit.Before;
|
|
|
|
+import org.junit.Test;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
+import java.util.concurrent.Executors;
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
+
|
|
|
|
+public class UploadTest {
|
|
|
|
+
|
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(UploadTest.class);
|
|
|
|
+
|
|
|
|
+ @Before
|
|
|
|
+ public void init() throws Exception {
|
|
|
|
+ // Configurator.setRootLevel(Level.INFO);
|
|
|
|
+ LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
|
|
|
|
+ ch.qos.logback.classic.Logger logger = loggerContext.getLogger("org.apache");
|
|
|
|
+ logger.setLevel(ch.qos.logback.classic.Level.INFO);
|
|
|
|
+ ch.qos.logback.classic.Logger logger2 = loggerContext.getLogger("com.aliyun");
|
|
|
|
+ logger2.setLevel(ch.qos.logback.classic.Level.INFO);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void demo() throws Exception {
|
|
|
|
+ OssConfig ossConfig = new OssConfig();
|
|
|
|
+ ossConfig.setBucket("apply-file");
|
|
|
|
+ ossConfig.setRegionId("oss-cn-shenzhen");
|
|
|
|
+ ossConfig.setEndpoint("https://oss-cn-shenzhen.aliyuncs.com");
|
|
|
|
+ ossConfig.setAccessKeyId("xxx");
|
|
|
|
+ ossConfig.setAccessKeySecret("xxx");
|
|
|
|
+ ossConfig.setUrlPrefix("https://apply-file.qmth.com.cn");
|
|
|
|
+
|
|
|
|
+ File dir = new File(ToolTest.DATA_DIR + "/17068-250528/test");
|
|
|
|
+ File[] files = dir.listFiles();
|
|
|
|
+
|
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
|
+ AtomicInteger finishAc = new AtomicInteger(), successAc = new AtomicInteger(), failAc = new AtomicInteger();
|
|
|
|
+ ExecutorService executorService = Executors.newFixedThreadPool(10);
|
|
|
|
+
|
|
|
|
+ final int totalCount = files.length;
|
|
|
|
+ for (File file : files) {
|
|
|
|
+ executorService.execute(() -> {
|
|
|
|
+ try {
|
|
|
|
+ OssFileInfo result = new OssHelper(ossConfig).writeFile(file.getName(), file);
|
|
|
|
+ int successCount = successAc.incrementAndGet();
|
|
|
|
+ log.debug("{} -> {} {}", successCount, result.getFileName(), result.getFileUrl());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("【图片上传】 {} err:{}", file.getName(), e.getMessage());
|
|
|
|
+ failAc.incrementAndGet();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int finishCount = finishAc.incrementAndGet();
|
|
|
|
+ if (finishCount % 100 == 0) {
|
|
|
|
+ float finishRate = finishCount * 100f / totalCount;
|
|
|
|
+ long cost = Math.max((System.currentTimeMillis() - startTime) / 1000, 1);
|
|
|
|
+ float speed = (float) finishCount / cost;
|
|
|
|
+ log.info("【图片上传】 总数:{} 成功数:{} 失败数:{} 进度:{}% 速度:约{}个每秒 已耗时:{}秒",
|
|
|
|
+ totalCount, successAc.get(), failAc.get(), finishRate, speed, cost);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ executorService.shutdown();
|
|
|
|
+ while (!executorService.isTerminated()) {
|
|
|
|
+ // ignore
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("文件上传已完成!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|