使用JAVA写一个程序下载网站和软件 单线程和多线程分段下载

本文详细介绍了如何利用JAVA编程实现单线程和多线程分段下载网站及软件的过程,涵盖了线程同步、文件合并等关键技术,对于提升下载效率和理解多线程编程具有指导意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Q721;
//断点续传
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Demo03 {
    public static void main(String[] args) {
        String path = "http://softforspeed.51xiazai.cn/down/BaiduNetdisk_6.9.7.4.exe";

        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);

          int  responseCode = conn.getResponseCode();
            System.out.println("responseCode = " + responseCode);
            //如果响应正确 200
            if (responseCode==200){
                //下载
                InputStream in = conn.getInputStream();
                String headerField = conn.getHeaderField("Content-Disposition");
                String field = headerField.split(";")[1];
                String fileName = field.substring(field.indexOf("\"")+1,field.lastIndexOf("\""));

                File file = new File(fileName);
                //读一组 写一组
                RandomAccessFile raf =new RandomAccessFile(file,"rwd");


                int len = 0;
                byte[]bytes=new byte[1024];
                while ((len = in.read(bytes))!=-1){
                    raf.write(bytes,0,len);
                }
                raf.close();
                in.close();
                System.out.println();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}





/**
   多线程下载
 * 下载文件的一段子线程
 * 至少要有三个属性
 * url
 * stort 文件分段的开始
 * end  文件分段的结束
 */
class  DownloadThread extends  Thread{
    private  String path;//网速
    private  long start;//开始的位置
    private long end;//结束的位置

    /**
     * 下载一段
     */
    @Override
    public void run() {
        String path = "http://softforspeed.51xiazai.cn/down/BaiduNetdisk_6.9.7.4.exe";
        //三个线程

        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);

            conn.setRequestProperty("Range","bytes="+start+"-"+end);

            int  responseCode = conn.getResponseCode();
            System.out.println("responseCode = " + responseCode);
            //如果响应正确 200
            if (responseCode==206){
                //下载
                int contentLength = conn.getContentLength();
                System.out.println("文件总大小 = " + contentLength);

                //切割为三分,计算每份的大小
                int size = contentLength/3;
                /*
                假如 contentLength=10 zize = 3
                线程1:
                线程2:
                线程3: size*2-contentLength
                 */
                for (int i = 0; i < 3; i++) {
                    DownloadThread dt = new DownloadThread();
                    dt.setName("线程");
                    dt.setPath(path);
                    dt.setStart(i*size);
                    if (i==2){
                        dt.setEnd(contentLength);
                    }else {
                        dt.setEnd((i*1)*size);
                    }
                    dt.start();
                }
                }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getPath() {
        return path;
    }

    public long getStart() {
        return start;
    }

    public long getEnd() {
        return end;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public void setEnd(long end) {
        this.end = end;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值