Java实现多线程下载文件

Java实现多线程下载文件,第1张

 这是本人在实际开发当中遇到的多线程下载文件并记录下来

public class DownloadUtil {
    private String pathFile;
    private String strFile;
    private DownloadThread[] downloadThreadArr;
    private int threadNum;
    private int size;

    public DownloadUtil (String pathFile, String strFile, int threadNum) {
        this.pathFile = pathFile;
        this.threadNum = threadNum;
        downloadThreadArr = new DownloadThread[threadNum];
        this.strFile = strFile;
    }

    public void download() throws Exception {
        URL url = new URL(pathFile);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty(
                "Accept",
                "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
                        + "application/x-shockwave-flash, application/xaml+xml, "
                        + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                        + "application/x-ms-application, application/vnd.ms-excel, "
                        + "application/vnd.ms-powerpoint, application/msword, */*");
        conn.setRequestProperty("Accept-Language", "zh-CN");
        conn.setRequestProperty("Charset", "UTF-8");
        conn.setRequestProperty("Connection", "Keep-Alive");

        size = conn.getContentLength();
        conn.disconnect();
        int currentPartSize = size / threadNum + 1;
        RandomAccessFile file = new RandomAccessFile(strFile, "rw");
        file.setLength(size);
        file.close();
        for (int i = 0; i < threadNum; i++) {
            int start = i * currentPartSize;
            RandomAccessFile currentPart = new RandomAccessFile(strFile, "rw");
            currentPart.seek(start);
            downloadThreadArr[i] = new DownloadThread(start, currentPartSize, currentPart);
            downloadThreadArr[i].start();
        }
    }

    
    class DownloadThread extends Thread {
        private int start;
        private int size;
        private RandomAccessFile out;
        public int length;
        
        public DownloadThread(int start, int size,RandomAccessFile out) {
            this.out = out;
            this.start = start;
            this.size = size;
        }

        @Override
        public void run() {
            InputStream is = null;
            try {
                URL url = new URL(pathFile);
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty(
                        "Accept",
                        "image/gif, image/jpeg, image/pjpeg, image/pjpeg, " + "application/x-shockwave-flash, application/xaml+xml, " + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                                + "application/x-ms-application, application/vnd.ms-excel, "
                                + "application/vnd.ms-powerpoint, application/msword, */*");
                conn.setRequestProperty("Accept-Language", "zh-CN");
                conn.setRequestProperty("Charset", "UTF-8");
                is = conn.getInputStream();
                is.skip(this.start);
                byte[] bs = new byte[1024];
                int len = 0;
                while ((len = is.read(bs)) != -1 && length < size) {
                    out.write(bs, 0, len);
                    length += len;
                }
                out.close();
                is.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    out.close();
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
               
            }
        }
    }

   
}







@RestController
@RequestMapping("tc")
public class TestController {

    @RequestMapping("/download")
    public void download(String filePath,String savePath) {
        try {
            DownloadUtil downloadUtil = new DownloadUtil (filePath,savePath,10);
            downloadUtil.download();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/992406.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存