1. 程式人生 > >獲取百度網盤下載真實地址

獲取百度網盤下載真實地址

這是一個java寫的獲取百度網盤真實下載連結進行下載的程式。
程式裡面一些引數拼接是根據瀏覽器抓包來的。具體的抓包方法網上一大堆,可以參考。這裡給出了原始碼和匯出的jar包。
url網址使用於百度分享的地址。暫時沒有適配有提取碼的地址。

執行的方法:
1、在當前的目錄開啟cmd,預設不帶引數就是用預設的測試的url。
java -jar BaiduPanURL.jar

2、如果想使用自己定義的資源地址可以在後面加一個引數。
java -jar BaiduPanURL.jar your_url

注意:百度網盤同一個ip進行3次下載之後就需要驗證碼。程式會在當前的目錄生成驗證碼,輸入正確的驗證碼後可以進行下載。

本人親測有效,百度會限制下載的速率,測試只有100kb左右。

程式原始碼:
1、GetBaiduCloudRealURL:

    package com.example.download;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.regex.Matcher;
    import java.util
.regex.Pattern; import com.example.bean.Response20; import com.example.bean.SetDataBean; import com.google.gson.Gson; /** * 輸入百度網盤的資源地址,獲取網盤的真實下載地址。 * * @author gaoqiang * */ public class GetBaiduCloudRealURL { // 定義一些全域性變數 private static String url = "https://pan.baidu.com/s/1eQrwbKY"
;// 資源地址 private static String cookie = null; private static final String getvcodeURL = "https://pan.baidu.com/api/getvcode?prod=pan";// 請求vcode地址,不變 private static boolean isDownload = true;//標記是否需要下載檔案,false就只獲取地址 private static String server_filename = null; private static String size = null; // 通過瀏覽器抓包分析,獲取百度網盤共享檔案的真實下載地址 public static void main(String[] args) { if (args.length > 0) { System.out.println("輸入的原始地址:" + args[0]); url = args[0]; } // 第一次獲取cookie() Map<String, String> map1 = HttpUtils.get(url, cookie); System.out.println("伺服器返回的cookie:\n" + map1.get("cookie") + "\n"); cookie = "PANWEB=1;" + map1.get("cookie").split(";")[0];// 抓包看到攜帶了PANWEB1,不設定也沒問題 Map<String, String> params = getBodyParams(map1.get("body")); server_filename = params.get("server_filename"); size = params.get("size"); // 拼接post的url地址 String post_url = getPostUrl(params); // 拼接post攜帶的引數 Map<String, String> data = getPostData(params); // 傳送post請求 String responseJson = HttpUtils.post(post_url, data, cookie); System.out.println(responseJson + "\n"); Gson gson = new Gson(); Response20 response20 = gson.fromJson(responseJson, Response20.class); String errorCode = response20.getErrno(); int count = 0; while (!errorCode.equals("0") && count < 5) { count++; if (errorCode.equals("-20")) { // 下載超過3次,需要驗證碼,獲取vcode和img地址 Map<String, String> generateValidateCode = generateValidateCode(count); data.put("vcode_input", generateValidateCode.get("vcode_input")); data.put("vcode_str", generateValidateCode.get("vcode_str")); String responseJsonCode = HttpUtils.post(post_url, data, cookie); System.out.println(responseJsonCode + "\n"); errorCode = gson.fromJson(responseJsonCode, Response20.class).getErrno(); if (errorCode.equals("0")) { responseJson = responseJsonCode; } } } if (errorCode.equals("0")) {// 成功返回真實的url String realURL = parseRealDownloadURL(responseJson); System.out.println("成功!真實的下載連結為:" + realURL); if (isDownload) { System.out.println("正在下載檔案..."); HttpUtils.download(realURL, cookie, server_filename, size);// 進行下載 }else{ System.out.println("配置不下載"); } } else { System.out.println("嘗試了" + count + "次,地址獲取失敗"); } } /** * POST請求的url地址 */ public static String getPostUrl(Map<String, String> params) { /** * post請求(抓包可看到) 抓包看到logid,實際測試logid不攜帶也可以正常抓取 * https://pan.baidu.com/api/ * sharedownload?sign=2d970c761500085deb09d423d549dea2f4ef28da * &timestamp= * 1515400310&bdstoken=null&channel=chunlei&clienttype=0&web=1 * &app_id=250528&logid=MTUxNTQwMDQ1NTc1MTAuOTYwMTE4NzIyMzcxMzYwNQ== */ StringBuffer sb1 = new StringBuffer(); sb1.append("https://pan.baidu.com/api/sharedownload?"); sb1.append("sign=" + params.get("sign")); sb1.append("&timestamp=" + params.get("timestamp")); sb1.append("&bdstoken=" + params.get("bdstoken")); sb1.append("&channel=chunlei"); sb1.append("&clienttype=0"); sb1.append("&web=1"); sb1.append("&app_id=" + params.get("app_id")); String post_url = sb1.toString(); System.out.println("POST請求的網址:" + post_url); return post_url; } /** * 獲取POST請求攜帶的引數 */ public static Map<String, String> getPostData(Map<String, String> params) { // POST攜帶的引數(抓包可看到) Map<String, String> data = new HashMap<String, String>(); data.put("encrypt", "0"); data.put("product", "share"); data.put("uk", params.get("uk")); data.put("primaryid", params.get("primaryid")); // 添加了[],解碼就是%5B %5D data.put("fid_list", "%5B" + params.get("fid_list") + "%5D"); data.put("path_list", "");// 可以不寫 return data; } /** * 從post返回資料解析出dlink欄位,真實的下載地址,這個地址有效期8h */ public static String parseRealDownloadURL(String responseJson) { String realURL = ""; Pattern pattern = Pattern.compile("\"dlink\":.*?,"); Matcher matcher = pattern.matcher(responseJson); if (matcher.find()) { String tmp = matcher.group(0); String dlink = tmp.substring(9, tmp.length() - 2); realURL = dlink.replaceAll("\\\\", ""); } return realURL; } /** * 獲取並輸入驗證碼 * * @return map{vcode_str:請求的vcode值, vcode_input:輸入的驗證碼} */ public static Map<String, String> generateValidateCode(int count) { // 下載超過3次,需要驗證碼,獲取vcode和img地址 Map<String, String> vcodeResponse = HttpUtils.get(getvcodeURL, cookie); String res = vcodeResponse.get("body"); System.out.println("獲取vcode:" + vcodeResponse.get("body")); Gson gsonVcode = new Gson(); Response20 responseVcode = gsonVcode.fromJson(res, Response20.class); String vcode = responseVcode.getVcode(); String imgURL = responseVcode.getImg(); System.out.println("vcode值:" + vcode); System.out.println("驗證碼地址:" + imgURL); // 請求驗證碼 HttpUtils.saveImage(imgURL, cookie); System.out.print("檢視圖片,輸入驗證碼(第" + count + "次嘗試/共5次):"); InputStreamReader is_reader = new InputStreamReader(System.in); Map<String, String> map = new HashMap<String, String>(); try { String result = new BufferedReader(is_reader).readLine(); System.out.println("輸入的驗證碼為:" + result + "\n"); map.put("vcode_str", vcode); map.put("vcode_input", result); } catch (IOException e) { e.printStackTrace(); } return map; } /** * 正則匹配出json字串,將json字串轉化為java物件。 */ public static Map<String, String> getBodyParams(String body) { Map<String, String> map = new HashMap<String, String>(); String setData = ""; Pattern pattern_setData = Pattern.compile("setData.*?;"); Matcher matcher_setData = pattern_setData.matcher(body); if (matcher_setData.find()) { String tmp = matcher_setData.group(0); setData = tmp.substring(8, tmp.length() - 2); // System.out.println("setData:" + setData + "\n"); Gson gson = new Gson(); SetDataBean bean = gson.fromJson(setData, SetDataBean.class); System.out.println("sign--" + bean.getSign()); System.out.println("token--" + bean.getBdstoken()); System.out.println("timestamp--" + bean.getTimestamp()); System.out.println("uk--" + bean.getUk()); System.out.println("shareid--" + bean.getShareid()); System.out.println("fs_id--" + bean.getFile_list().getList()[0].getFs_id()); System.out.println("file_name--" + bean.getFile_list().getList()[0].getServer_filename()); System.out.println("size--" + bean.getFile_list().getList()[0].getSize()); System.out.println("app_id--" + bean.getFile_list().getList()[0].getApp_id() + "\n"); map.put("sign", bean.getSign()); map.put("timestamp", bean.getTimestamp()); map.put("bdstoken", bean.getBdstoken()); map.put("app_id", bean.getFile_list().getList()[0].getApp_id()); map.put("uk", bean.getUk()); map.put("shareid", bean.getShareid()); map.put("primaryid", bean.getShareid()); map.put("fs_id", bean.getFile_list().getList()[0].getFs_id()); map.put("fid_list", bean.getFile_list().getList()[0].getFs_id()); map.put("server_filename", bean.getFile_list().getList()[0].getServer_filename()); map.put("size", bean.getFile_list().getList()[0].getSize()); } return map; } }

2、HttpUtils:

    package com.example.download;

    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.HashMap;
    import java.util.Map;

    public class HttpUtils {
        /**
         * 向指定URL傳送GET方法的請求 返回cookie,body等
         * 會返回流裡面的內容,如果下載大檔案,需要修改,實時儲存,避免記憶體溢位
         */
        public static Map<String, String> get(String url, String cookie) {
            BufferedReader in = null;
            Map<String, String> map = new HashMap<String, String>();

            try {
                URL realUrl = new URL(url);
                // 開啟和URL之間的連線
                URLConnection connection = realUrl.openConnection();
                // 設定通用的請求屬性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                if (null != cookie) {
                    //System.out.println("攜帶的Cookie:" + cookie);
                    connection.setRequestProperty("Cookie", cookie);
                }
                // 建立實際的連線
                connection.connect();
                // 定義 BufferedReader輸入流來讀取URL的響應
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = in.readLine()) != null) {
                    sb.append(line + "\n");
                    //System.out.println("內容:" + line);
                }
                String c = connection.getHeaderField("Set-Cookie");
                map.put("cookie", c);
                map.put("body", sb.toString());
                return map;
            } catch (Exception e) {
                System.err.println(e.getMessage());
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return null;
        }

        /**
         * 儲存驗證碼
         */
        public static Map<String, String> saveImage(String url, String cookie) {
            Map<String, String> map = new HashMap<String, String>();
            InputStream in = null;
            FileOutputStream fos = null;
            try {
                URL realUrl = new URL(url);
                // 開啟和URL之間的連線
                URLConnection connection = realUrl.openConnection();
                // 設定通用的請求屬性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                if (null != cookie) {
                    connection.setRequestProperty("Cookie", cookie);
                }
                // 建立實際的連線
                connection.connect();

                in = connection.getInputStream();
                fos = new FileOutputStream("img.jpeg");

                int b;
                while((b= in.read())!=-1){
                    fos.write(b);
                }

                return map;
            } catch (Exception e) {
                System.err.println(e.getMessage());
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                    if(fos != null){
                        fos.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return null;
        }


        static int downloadSize = 0;
        static boolean isReading = false;//是否在讀流,更新進度
        static long startTime = 0 ;
        static long endTime = 0 ;
        /**
         * 下載檔案
         */
        public static Map<String, String> download(String url, String cookie, String filename, final String totalSize) {
            InputStream in = null;
            FileOutputStream fos = null;
            Map<String, String> map = new HashMap<String, String>();
            try {
                URL realUrl = new URL(url);
                // 開啟和URL之間的連線
                URLConnection connection = realUrl.openConnection();
                // 設定通用的請求屬性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                connection.setConnectTimeout(10000);
    //          connection.setReadTimeout(5000);
                if (null != cookie) {
                    connection.setRequestProperty("Cookie", cookie);
                }
                // 建立實際的連線
                connection.connect();

                in = connection.getInputStream();
                fos = new FileOutputStream(filename);

                System.out.println("儲存檔名稱:" + filename);
                System.out.println("檔案總大小:" + totalSize);
                //利用字元陣列讀取流資料

                startTime = System.currentTimeMillis();

                //每隔1s讀一次資料
                new Thread(){
                    @Override
                    public void run() {
                        String total = UnitSwitch.formatSize(Long.parseLong(totalSize));
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < 20; i++) {
                            sb.append("\b");
                        }
                        while(isReading){
                            endTime = System.currentTimeMillis();
                            String speed = UnitSwitch.calculateSpeed(downloadSize, endTime-startTime);
                            System.out.println(UnitSwitch.formatSize(downloadSize) + "/" + total + ",平均下載速率:" + speed);
                            try {
                                sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }.start();

                isReading = true;
                int len;
                byte[] arr = new byte[1024 * 8];
                while((len = in.read(arr)) != -1) {
                    fos.write(arr, 0, len);
                    downloadSize += len;
                }

                endTime = System.currentTimeMillis();
                String speed = UnitSwitch.calculateSpeed(downloadSize, endTime-startTime);
                String total = UnitSwitch.formatSize(Long.parseLong(totalSize));
                System.out.println(UnitSwitch.formatSize(downloadSize) + "/" + total + ",平均下載速率:" + speed);
                System.out.println("下載完成,總耗時:" + (endTime - startTime)/1000 + "秒");
                return map;
            } catch (Exception e) {
                System.err.println(e.getMessage());
            } finally {
                isReading = false;
                try {
                    if (in != null) {
                        in.close();
                    }
                    if(fos != null){
                        fos.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return null;
        }

        /**
         * 傳送HttpPost請求
         * 
         * @param strURL
         *            服務地址
         * @param params
         * 
         * @return 成功:返回json字串<br/>
         */
        public static String post(String strURL, Map<String, String> params, String cookie) {
            try {
                URL url = new URL(strURL);// 建立連線
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestMethod("POST"); // 設定請求方式
                connection.setRequestProperty("Accept", "*/*"); // 設定接收資料的格式
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // 設定傳送資料的格式
                if (null != cookie) {
                    System.out.println("攜帶的Cookie:" + cookie);
                    connection.setRequestProperty("Cookie", cookie);
                }

                connection.connect();
                OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8編碼

                StringBuffer sb = new StringBuffer();
                for (String s : params.keySet()) {
                    sb.append(s + "=" + params.get(s) + "&");
                }
                System.out.println("攜帶的引數:" + sb.toString().substring(0, sb.length() - 1));
                out.append(sb.toString().substring(0, sb.length() - 1));
                out.flush();
                out.close();

                int code = connection.getResponseCode();
                BufferedReader in = null;
                if (code == 200) {
                    in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                } else {
                    in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                }
                System.out.println("響應碼:" + code);

                // 定義BufferedReader輸入流來讀取URL的響應
                String line;
                StringBuffer sb1 = new StringBuffer();
                while ((line = in.readLine()) != null) {
                    sb1.append(line);
                }
                return sb1.toString();

            } catch (IOException e) {
                System.err.println(e.getMessage());
                return "error";
            }
        }
    }

3、UnitSwitch:

package com.example.download;
import java.text.DecimalFormat;
public class UnitSwitch {
    /**
     * 處理檔案大小
     */
    public static String formatSize(long size) {
        DecimalFormat fnum = new DecimalFormat("##0.0");
        String result;

        if (size > (1024 * 1024 * 1024)) {
            result = fnum.format((size / ((float) (1024 * 1024 * 1024)))) + "GB";
        }else if (size > (1024 * 1024)) {
            result = fnum.format((size / ((float) (1024 * 1024)))) + "MB";
        } else if (size > 1024) {
            result = fnum.format((float) size / ((float) 1024)) + "KB";
        } else {
            result = String.valueOf((int) size) + "B";
        }
        return result;
    }

    /**
     * @author gaoqiang
     *         <p>
     *         計算網路速率
     *         <p>
     *         返回的格式 Kb/s
     * @param delta
     *            : 傳入一個Bytes資料
     * @param duration_time
     *            : 下載delta資料所需要用的時間
     * @return
     */
    public static String calculateSpeed(long delta, long duration_time) {
        DecimalFormat fnum = new DecimalFormat("##0.0");
        String speedStr;
        float speed = ((float) delta * 1000 / (float) (duration_time));// 毫秒轉換

        if (speed > (1024 * 1024)) {
            speedStr = fnum.format((speed / ((float) (1024 * 1024)))) + "M/s";
        } else if (speed > 1024) {
            speedStr = fnum.format((float) speed / ((float) 1024)) + "K/s";
        } else {
            speedStr = String.valueOf((int) speed) + "B/s";
        }
        return speedStr;
    }
}

4、Response20:

    package com.example.bean;

    /**
     * 伺服器返回的json,轉java物件
     * 錯誤碼為20 代表需要驗證碼
     * @author gaoqiang
     *
     */
    public class Response20 {
        String errno;
        String request_id;
        String server_time;

        String vcode;
        String img;

        public String getErrno() {
            return errno;
        }

        public void setErrno(String errno) {
            this.errno = errno;
        }

        public String getRequest_id() {
            return request_id;
        }

        public void setRequest_id(String request_id) {
            this.request_id = request_id;
        }

        public String getServer_time() {
            return server_time;
        }

        public void setServer_time(String server_time) {
            this.server_time = server_time;
        }

        public String getVcode() {
            return vcode;
        }

        public void setVcode(String vcode) {
            this.vcode = vcode;
        }

        public String getImg() {
            return img;
        }

        public void setImg(String img) {
            this.img = img;
        }

    }

5、SetDataBean:

    package com.example.bean;

    public class SetDataBean {

        private String sign;
        private String timestamp;
        private String bdstoken;
        private String uk;
        private String shareid;
        private FileList file_list;


        public String getSign() {
            return sign;
        }

        public void setSign(String sign) {
            this.sign = sign;
        }

        public String getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }

        public String getBdstoken() {
            return bdstoken;
        }

        public void setBdstoken(String bdstoken) {
            this.bdstoken = bdstoken;
        }

        public String getUk() {
            return uk;
        }

        public void setUk(String uk) {
            this.uk = uk;
        }

        public String getShareid() {
            return shareid;
        }

        public void setShareid(String shareid) {
            this.shareid = shareid;
        }

        public FileList getFile_list() {
            return file_list;
        }

        public void setFile_list(FileList file_list) {
            this.file_list = file_list;
        }

        public static class FileList {
            String errno;
            List[] list;

            public String getErrno() {
                return errno;
            }

            public void setErrno(String errno) {
                this.errno = errno;
            }

            public List[] getList() {
                return list;
            }

            public void setList(List[] list) {
                this.list = list;
            }

        }

        public static class List {
            String app_id;
            String fs_id;
            String server_filename;
            String size;

            public String getApp_id() {
                return app_id;
            }

            public void setApp_id(String app_id) {
                this.app_id = app_id;
            }

            public String getFs_id() {
                return fs_id;
            }

            public void setFs_id(String fs_id) {
                this.fs_id = fs_id;
            }

            public String getServer_filename() {
                return server_filename;
            }

            public void setServer_filename(String server_filename) {
                this.server_filename = server_filename;
            }

            public String getSize() {
                return size;
            }

            public void setSize(String size) {
                this.size = size;
            }

        }

    }

6、執行的截圖:

執行的引數圖:
這裡寫圖片描述

效果圖:
這裡寫圖片描述