多线程请求,在浏览器模拟请求间隔4次发现多线程下载的文件就是0字节(原文件大小46M)了。这是为什么?怎么修改?

wangxi得 发布于 08/28 17:15
阅读 41
收藏 0
package com.dahuatech.scene.config;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.PreDestroy;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ThreadPoolConfig {


    @Bean(name = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(200);// 核心线程池大小
        executor.setCorePoolSize(50);// 最大可创建的线程数
        executor.setQueueCapacity(1000); // 队列最大长度
        executor.setKeepAliveSeconds(300);  // 线程池维护线程所允许的空闲时间
        // 线程池对拒绝任务(无线程可用)的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }



}
@GetMapping("/testPool")
    public String testPool(){
        log.info("1");
        MyTask myTask = new MyTask(null, 0, "https://10.96.9.179/evo-apigw/evo-httpnode/vod/cam/download.mp4?vcuid=1000068%240&subtype=1&starttime=2024_8_20_07_00_00&endtime=2024_8_20_07_01_00&videoType=3&token=1:MMqrKBRD1X3DoJUFyZ3c5YRar03KlSA4&recordType=1");
        executor.execute(myTask);
//        executor.shutdown();
        return "";
    }
    class MyTask implements Runnable {
        String code;
        int i;
        String baseUrl;

        MyTask(String code, int i, String baseUrl) {
            this.baseUrl = baseUrl;
            this.i = i;
            this.code = code;
        }

        @Override
        public void run() {
            BufferedInputStream bis = null;
            FileOutputStream fis = null;
            try{
                URL url = new URL(baseUrl);
                trustAllHosts();
                HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
                connection.setHostnameVerifier(DO_NOT_VERIFY);
                bis = new BufferedInputStream(connection.getInputStream());
                String osName = System.getProperty("os.name");
                String path = FileUtils.getSysPath("video",System.currentTimeMillis());
                fis = new FileOutputStream(path + "1.mp4"  );
                byte[] buffer = new byte[1024];
                int count = 0;
                while ((count = bis.read(buffer, 0, 1024)) != -1) {
                    fis.write(buffer, 0, count);
                }
                log.info("Download completed: " + path);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    if (bis != null) {
                        bis.close();
                    }
                    if (fis != null) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部