0721题

package com.hp.cjh;

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 Demo01 extends Thread{
    private int start;//起始位置
    private int end;//结束位置
    private static final String filedz="C:\\Users\\Administrator\\Desktop\\每日记录\\aa.exe";

    @Override
    public void run() {
        //创建一个URL对象
        String path="http://softforspeed.51xiazai.cn/down/BaiduNetdisk_6.9.7.4.exe";
        try {
            URL url = new URL(path);
            //打开连接,获取了url请求的连接对象conn
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //请求设置
            conn.setConnectTimeout(5000);//请求的超时时间
            //读取超时时间
            conn.setReadTimeout(5000);
            //设置请求提交的方法Get Post
            conn.setRequestMethod("GET");
            //设置线程本次断点续传文件范围
            conn.setRequestProperty("Range","bytes="+start+"-"+end);//获取响应码
            int responseCode = conn.getResponseCode();//开始道结束
            //获取响应码
            if(responseCode==206){//由于是部分数据 206就表示ok
                //下载本段文件(参考课堂案例下载代码)
                InputStream in = conn.getInputStream();//输入流
                //下载的流
//                String filename = conn.getHeaderField("c:\\mm");//获取响应头部文件(包含由文件名)
//                filename=filename.split(";")[1];
//                filename=filename.substring(filename.indexOf("\"")+1,filename.lastIndexOf("\""));
//                File file=new File(filename);


                //设置本次下载的部分数据从哪个位置开始RandomAccessFile的seek方法
                RandomAccessFile ra = new RandomAccessFile(filedz, "rwd");//输出流
                ra.seek(start);//实现断点续传的核心方法   寻找起始位置
                System.out.println("当前线程:"+"从"+start+"开始,到"+end+"结束");
                // ....
                byte[] bytes = new byte[1024];
                int len=0;
                while ((len=in.read(bytes))!=-1){//不等于-1 说明没有读完,会继续读
                    ra.write(bytes,0,len);
                }
                System.out.println("当前线程下载完成");

                //判断关流
                if(ra!=null){
                    ra.close();
                }
                if(in!=null){
                    in.close();
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //get和set省略...

    public int getStart() {
        return start;
    }

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

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }
}
package com.hp.cjh;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Demo02 {
    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 cnn = (HttpURLConnection) url.openConnection();
            //设置请求
            cnn.setConnectTimeout(5000);//请求超时时间
            cnn.setReadTimeout(5000);//读取超时时间
            cnn.setRequestMethod("GET");//设置请求提交的方法
            int responseCode = cnn.getResponseCode();//获取响应码
            System.out.println("responseCode = " + responseCode);
            //如果连接ok  200
            if(responseCode==200){
                //设计分段下载(如分成3次下载)
                int contentLength = cnn.getContentLength();//总长度
                //获取它的长度
                System.out.println("contentLength = " + contentLength);
                //三部分  每一部分长度/大小
                int i = contentLength/3;
                //开启线程执行下载
                for (int j = 0; j < 3; j++) {
                    //创建断点传线程(下载一部分)

                    InputStream in = cnn.getInputStream();
                    //设置下载那一部分(start,end)
                    Demo01 demo01 = new Demo01();
                    demo01.setStart(j*i);
                    demo01.setEnd((j+1)*i);
                    //线程对象那。start()
                    demo01.start();//告诉系统,我已经准备好了
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package com.hp.cjh;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
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();
            InputStream in = conn.getInputStream();
            FileOutputStream out = new FileOutputStream("D:\\BaiduNetdisk_6.9.7.4.exe");
            byte[] bytes = new byte[1024];
            int len=-1;
            while ((len=in.read(bytes))!=-1){
                out.write(bytes,0,len);
            }
            out.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值