百度网盘离线下载分析及实现

离线任务列表
http://pan.baidu.com/rest/2.0/services/cloud_dl?bdstoken=1bb1c4a7c55f4aad265443ba5fc63112&need_task_info=1&status=255&start=0&limit=10&method=list_task&app_id=250528&t=1441305270646&bdstoken=1bb1c4a7c55f4aad265443ba5fc63112&channel=chunlei&clienttype=0&web=1&app_id=250528

http://pan.baidu.com/rest/2.0/services/cloud_dl
?bdstoken=1bb1c4a7c55f4aad265443ba5fc63112
&need_task_info=1 //需要离线的信息
&status=255 //查询全部状态的(包括0和1 即下载完成的0和未完成的1)
&start=0 // 状态为1表示未下载成功,
&limit=10 //线程为10
&method=list_task // 查询离线任务列表
&app_id=250528 //百度网盘appid
&t=1441305270646 //本级所在目录的id
&bdstoken=1bb1c4a7c55f4aad265443ba5fc63112&channel=chunlei&clienttype=0&web=1&app_id=250528
获得task_id后,利用task_id提交到这个url获取详细的信息,没看懂具体的用处

http://pan.baidu.com/rest/2.0/services/cloud_dl?bdstoken=1bb1c4a7c55f4aad265443ba5fc63112&task_ids=843412236%2C843412240%2C843412215&op_type=1&method=query_task&app_id=250528&t=1441304438011&bdstoken=1bb1c4a7c55f4aad265443ba5fc63112&channel=chunlei&clienttype=0&web=1&app_id=250528

http://pan.baidu.com/rest/2.0/services/cloud_dl
?bdstoken=1bb1c4a7c55f4aad265443ba5fc63112
&task_ids=843412236%2C843412240%2C843412215
&op_type=1
&method=query_task
&app_id=250528
&t=1441304438011

&bdstoken=1bb1c4a7c55f4aad265443ba5fc63112
&channel=chunlei
&clienttype=0
&web=1
&app_id=250528

//查询某个目录下面文件
=1441305110157&bdstoken=1bb1c4a7c55f4aad265443ba5fc63112&channel=chunlei&clienttype=0&web=1&app_id=250528”>http://pan.baidu.com/api/list?channel=chunlei&clienttype=0&web=1&num=100&page=1&dir=%2Ftmp&order=time&desc=1&showempty=0&=1441305110157&bdstoken=1bb1c4a7c55f4aad265443ba5fc63112&channel=chunlei&clienttype=0&web=1&app_id=250528

分析:
http://pan.baidu.com/api/list
?channel=chunlei
&clienttype=0 //客户端类型 0为web
&web=1 //web类型,1表示是,0表示否,为1是有时会要求输入验证码,这里通常改为0
&num=100 //每页显示的数量
&page=1 //当前的页数
&dir=%2Ftmp //当前的文件夹,
&order=time //按时间排序 默认倒序,
&desc=1 //倒序排序,0为正序,即由小到大,通常不会这么做
&showempty=0
&_=1441305110157 //当前目录的id号
&bdstoken=1bb1c4a7c55f4aad265443ba5fc63112
&channel=chunlei
&clienttype=0
&web=1
&app_id=250528

我抓的1万2千条的url,本来想用这种方式直接全部离线,后来发现不对
这个本来想做返回值处理的,没做好就被达到离线最大值了,禁止离线了,等测试好了再发

,我的想法就是想办法来判断下现在正在下载的如果等于4就直接暂停,然后等几秒再次获取正在下载的任务数,如果小于4就可以进行下一次的下载,如果等于5,下次下载会失败返回409,对于那个url的处理,好像也是有问题的


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Test {
    private static BufferedReader br;
    public static void main(String[] args) {
        File file = new File("C:\\Users\\xuegod1506\\workspace\\Test\\src\\list.txt");
        FileInputStream fis;
        try {
            fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            String lines;
            while ((lines = br.readLine()) != null) {
                System.out.println(lines);
                while(!downLoad(lines)){
                    Thread.sleep(1000);
                    downLoad(lines);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    private static boolean downLoad(String sourceUrl){
        boolean result = true;
        sourceUrl=sourceUrl.replaceAll(" ", "%20").replaceAll("/", "%2F").replaceAll("#", "%23").replaceAll("&", "%26").replaceAll("=", "%3D").replaceAll(":", "%3A");
        String str = "http://pan.baidu.com/rest/2.0/services/cloud_dl?bdstoken=1bb1c4a7c55f4aad265443ba5fc63112"
                + "&app_id=250528"
                + "&method=add_task"
                + "&clienttype=1"
                + "&web=0"
                + "&save_path=%2Ftmp%2F"
                + "&source_url="+sourceUrl;
        String cookie = "";
        try {
            URL url = new URL(str);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            connection.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0");
            connection.setRequestProperty("Cookie", cookie);
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String lines;
            StringBuffer sb = new StringBuffer("");
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes());
                sb.append(lines);
            }
            System.out.println(sb);
            reader.close();
            connection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

}

你可能感兴趣的:(JAVA开发)