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

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

這是一個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;
        }


    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234

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";
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

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;
        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

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;
            }

        }

    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125

6、執行的截圖:

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

效果圖: 
這裡寫圖片描述

相關推薦

JAVA獲取下載真實地址

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

獲取下載真實地址

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

java獲取真實下載連結

1. [程式碼]獲取方法 import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.M

獲取真是下載地址

time ole 不想 https _id 地址 360瀏覽器 data upload 1、將要下載的文件分享出去,是公開分享,私密不行 2、打開分享後的頁面,按下f12進入調試模式,在控制臺輸入一下代碼,點擊回車,既可看到真實下載地址 var ur

下載助手的用法

百度網盤 直接下載 首先需要安裝一個腳本管理器打開網址:https://greasyfork.org/zh-CN 根據你的瀏覽器選擇下載,像QQ瀏覽器,360極速等都是跟Chrome內核一樣的下載好後,在上面搜索框輸入“百度網盤直接下載助手”選擇第一個安裝安裝好後點擊剛剛安裝的腳本管理器,選擇管理面板

Aria2 下載教程

ria rom 1-1 avi 打包 連接 剛才 exp fill 感謝: https://www.chiphell.com/thread-1228502-1-1.html 迅雷離線下載需要迅雷會員功能 轉戰百度雲 打開如下連接 https://github.com/acg

分享個下載工具

too app cati 工具箱 lan sky png http 工具 註意:本篇博客用IE瀏覽器或者edage瀏覽器打開 1,安裝.netframework4.5 點我(以前裝了就不用了) 2, 安裝工具箱(IE瀏覽器) 點我 3,打開工具箱搜索

史上最強下載限速破解方法大全

文章有不當之處,歡迎指正,如果喜歡微信閱讀,你也可以關注我的微信公眾號:java大資料修煉之道,獲取優質學習資源。 一、概述 宣告:這個主要是從網路中收集到的目前的方法,僅供學習參考。。。哈哈!! 我們都知道百度雲是我們經常使用的軟體之一,但是現在使用它,下載速度確實很感人,就算是你買了會

解除下載限速

眾所周知的百度網盤為了迫使使用者花錢,對使用者的下載速度做出了限制。今天我就來分享一個破解限制的好辦法,這個也是網上扒來的。 準備工作 1、下載相關資料,也就是兩個檔案:http://pan.baidu.com/s/1nuPgpNN 2、安裝chrome瀏覽器 3、安裝下載的

下載助手

使用該方法的背景:公司不讓使用網盤類的東西 解決問題:(1)為了學習,(2)下載大檔案學習資源,(3)解決百度限速問題,(4)網頁版本 資料夾下載 解決步驟:  (1)先安裝油猴,(2)在安裝個百度下載助手  油猴下載地址(重要官網):https://tamp

Mac上利用Aria2加速下載

百度網盤下載東西的速度那叫一個慢,特別是大檔案,看著所需時間幾個小時以上,讓人很不舒服,本文記錄自己在mac上利用工具Aria2加速的教程,windows下思路也是一樣! 科普(可以不看) 這裡順帶科普一下,有時候我們上傳一個大檔案,會發現是秒傳,特別是某些電影,其實很簡單,百度網盤做了優化,所

下載神器Pan Download和SpeedPan,簡單幾步讓電腦滿速下載檔案

大家都知道,百度網盤如果不是超級會員的話,下載速度會非常慢,慢到你懷疑人生,幾十M的檔案都要下載好久。不過說實話,對於經常使用百度網盤下載東西的人來說,一個月30塊錢的超級會員費用並不貴,畢竟現在頻寬這麼貴,百度又不是慈善家,肯定要考慮收回成本和盈利的問題。 可是對於我們這些一個月只用幾次百

java功能

java 版百度網盤功能,目前已經實現: 1:百度網盤登入 2:列出百度網盤檔案 3: 切換目錄  4: 多執行緒下載檔案 速度有待優化。思路已經成型。 原始碼地址:https://gitee.com/xiaoyaofeiyang/BaiduPcs #

IDM下載器使用方法詳解:下載,視頻會員一網打盡!

安裝 img 慢慢 彈出 out 雲資源 註冊 載器 加密方式 一. IDM的設置 【01】 IDM插件與各大瀏覽器的集成 默認情況下,在成功安裝IDM後,直接點擊這裏的選項,會彈出【常規設置】,一般情況下直接保持默認的配置即可,如果你使用的是比較小眾的瀏覽器,你可以點擊這

【破解 SVIP】提升下載速度的另一種方法

友情提示,如果不好使,可以看看其他幾篇: 其他幾個類似工具: 【high-speed-downloader】支援 Mac 和 Windows, 【proxyee-down】支援 Mac 和 Windows, 【pandownload】僅支援 Windows 。 參見: 【

下載限速如何破解?

我們都知道百度雲是我們經常使用的軟體之一,但是現在使用它,下載速度確實很感人,就算是你買了會員,速度也並不是很快,所以今天教大家幾個方法,不管你是手機還是電腦,都可以輕鬆下載百度雲。   Android:ES 檔案管理器 + ADM Pro   ADM Pro

mac實現下載不限速(附React資源)

今天需要在百度網盤上下載react的學習資料,但是下載的速度太慢了,就尋思著找一個能快速下載百度網盤資源的方法。   ▍使用工具 百度網盤不限速外掛:https://pan.baidu.com/s/1xJyV-8hPUma7PWSGG8By-A 提取碼: yfe5 

Mac版Adobe Photoshop CC 2018中文安裝破解圖文教程(附下載地址

Mac版Adobe Photoshop CC 2018中文安裝破解圖文教程   釋出時間:2017-10-26 11:19:00   作者:佚名    我要評論 Adobe Photoshop CC 2018 for Mac最新中文破解版是一款蘋果電腦版的影象處理軟體,P

下載不限速工具 速v1.61

速盤是一款基於aria2技術,輔助使用者下載百度網盤資源的網盤下載工具。除了擁有不限速下載特點之外,速盤和其他的百度網盤下載器不同之處在於,速盤不用登入百度賬號,就可以高速下載百度網盤的分享連結,告別封號風險。此外,它還獨有百度網盤資源搜尋功能,同樣無需登入,搜尋資源

推薦一款實用的下載神器

推薦一款百度網盤下載神器之前有人使用我曾經推薦過的百度網盤加速器Pan Download,最近跟我反饋,不能使用了,我趕緊去開啟看一下,果真被作者關停了,一開啟軟體就提示:由於個別黑心商家將本免費軟體有償售賣,即日起暫停軟體使用 。如下圖:對於經常使用百度網盤的人,這軟體不能