Application.java 4.61 KB
package com.vion;


import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.ObjectListing;
import com.vion.entity.OssClientEntity;
import com.vion.utils.OssFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;


@ComponentScan(value = "com.vion")
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@Slf4j
public class Application implements CommandLineRunner {

    public static final String PICTURE = "picture/";
    public static final String FEATURE = "feature/";
    public static final String POSTFIX = ".feature";


    public static void main(String[] args) {
        try {
            SpringApplication.run(Application.class, args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //    @Autowired
    //    private IDao dao;
    @Resource
    private OssClientEntity ossClientEntity;

    @Value("${path.picture}")
    private String facePath;
    @Value("${path.feature}")
    private String featurePath;
    @Value("${date.begin}")
    private String begin;
    @Value("${date.end}")
    private String end;
    @Value("${prefix}")
    private String prefix;
    @Value(("${serialnumStr:}"))
    private String serialnumStr;

    @Override
    public void run(String... args) throws Exception {
        //        File file = new File(facePath);
        //        if (!file.exists()) {
        //            log.error("face路径不存在!!!!!:{}", facePath);
        //            System.exit(1);
        //        }
        //        File file1 = new File(featurePath);
        //        if (!file1.exists()) {
        //            log.error("feature路径不存在!!!!!!!!!!{}", featurePath);
        //            System.exit(2);
        //        }

        List<String> filePathList = new ArrayList<>();
        if (!serialnumStr.isEmpty()) {
            String[] serialnums = serialnumStr.split(",");
            for (String serialnum : serialnums) {
                filePathList.add(prefix + serialnum + "/");
            }
        } else {
            filePathList.add(prefix);
        }


        for (String filePath : filePathList) {


        /*下载文件*/
            OssFileUtil ossFileUtil = new OssFileUtil();
            ObjectListing objectListing = null;
            do {
                ListObjectsRequest request = ossFileUtil.getFilesInPrefix(ossClientEntity.getBucket(), filePath, ossClientEntity.getOssClient(), 500, false);
                if (objectListing != null) {
                    request.setMarker(objectListing.getNextMarker());
                }

                objectListing = ossClientEntity.getOssClient().listObjects(request);

                objectListing.getObjectSummaries().forEach(summery -> {
                    File f = new File("D:\\" + summery.getKey().replace("\\", "/"));
                    if (!f.getParentFile().exists()) {
                        f.getParentFile().mkdirs();
                    }
                    if (!f.exists()) {
                        System.out.println("正在下载" + summery.getKey());
                        ossClientEntity.getOssClient().getObject(new GetObjectRequest(ossClientEntity.getBucket(), summery.getKey()), f);

                    } else {
                        System.out.println(summery.getKey() + "已存在,跳过-------------");
                    }

                }
                );
            } while (objectListing.isTruncated());

        }
        //上传文件
        // 建立线程池
        //
        //        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(50, 100, 10, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(250), r -> {
        //            Thread t = new Thread(r);
        //            t.setName("线程" + OssConfigration.COUNT);
        //            OssConfigration.COUNT++;
        //            return t;
        //        });
        //
        //
        //        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        //        Date beginDate = format.parse(begin);
        //        Date endDate = format.parse(end);
        //
        //        dao.getAll(beginDate, endDate, ossClientEntity, facePath, featurePath, threadPool);
    }
}