java获取百度网盘真实下载链接的方法

作者:王滔 时间:2021-09-07 21:16:08 

本文实例讲述了java获取百度网盘真实下载链接的方法。分享给大家供大家参考。具体如下:

目前还存在一个问题,同一ip在获取3次以后会出现验证码,会获取失败,感兴趣的朋友对此可以加以完善。

返回的List<Map<String, Object>>  中的map包含:fileName( 文件名),url(实链地址)

HttpRequest.java如下:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequest {
public static String getData(String u) throws Exception {
 String re="";
   URL url = new URL(u);
 HttpURLConnection httpURLConnection = (HttpURLConnection) url
   .openConnection();
 httpURLConnection.setRequestMethod("GET");
 httpURLConnection.setDoInput(true);
 httpURLConnection.setDoOutput(true);
 InputStream is = httpURLConnection.getInputStream();
 InputStreamReader isr = new InputStreamReader(is);
 BufferedReader bufferedReader = new BufferedReader(isr);
 String iL = "";
 while ((iL = bufferedReader.readLine()) != null) {
  re += iL + "\n";
 }
 return re;
}
}

获取方法:


import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class BaiduNetDisk {
public static List<Map<String, Object>> getUrl(String url) throws Exception {
 List<String> fs_id = new ArrayList<String>();
 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 Document doc = Jsoup.connect(url).get();
 String html = doc.toString();
 int a = html.indexOf("{\"typicalPath");
 int b = html.indexOf("yunData.getCon");
 int sign_head = html.indexOf("yunData.SIGN = \"");
 int sign_foot = html.indexOf("yunData.TIMESTAMP");
 int time_head = html.indexOf("yunData.TIMESTAMP = \"");
 int time_foot = html.indexOf("yunData.SHARE_UK");
 int share_id_head = html.indexOf("yunData.SHARE_ID = \"");
 int share_id_foot = html.indexOf("yunData.SIGN ");
 String sign = html.substring(sign_head, sign_foot);
 sign = sign.substring(sign.indexOf("\"") + 1, sign.indexOf("\";"));
 String time = html.substring(time_head, time_foot);
 time = time.substring(time.indexOf("\"") + 1, time.indexOf("\";"));
 String share_id = html.substring(share_id_head, share_id_foot);
 share_id = share_id.substring(share_id.indexOf("\"") + 1,
   share_id.indexOf("\";"));
 System.out.println(share_id);
 html = html.substring(a, b);
 a = html.indexOf("{\"typicalPath");
 b = html.indexOf("};");
 JSONArray jsonArray = new JSONArray("[" + html.substring(a, b + 1)
   + "]");
 JSONObject jsonObject = jsonArray.getJSONObject(0);
 String uk = jsonObject.getString("uk");
 String shareid = jsonObject.getString("shareid");
 String path = URLEncoder.encode(jsonObject.getString("typicalPath"),
   "utf-8");
 jsonArray = new JSONArray("[" + jsonObject.getString("file_list") + "]");
 jsonObject = jsonArray.getJSONObject(0);
 jsonArray = new JSONArray(jsonObject.getString("list"));
 jsonObject = jsonArray.getJSONObject(0);
 String app_id = jsonObject.getString("app_id");
 if (jsonObject.getString("isdir").equals("1")) {
  String url1 = "http://pan.baidu.com/share/list?uk="
    + uk
    + "&shareid="
    + shareid
    + "&page=1&num=100&dir="
    + path
    + "&order=time&desc=1&_="
    + time
    + "&bdstoken=c51077ce0e0e313a16066612a13fbcd4&channel=chunlei&clienttype=0&web=1&app_id="
    + app_id;
  String fileListJson = HttpRequest.getData(url1);
  System.out.println(fileListJson);
  jsonArray = new JSONArray("[" + fileListJson + "]");
  jsonObject = jsonArray.getJSONObject(0);
  jsonArray = new JSONArray(jsonObject.getString("list"));
 }
 final int size = jsonArray.length();
 for (int i = 0; i < size; i++) {
  Map<String, Object> map = new HashMap<String, Object>();
  jsonObject = jsonArray.getJSONObject(i);
  String fileName = jsonObject.getString("server_filename");
  map.put("fileName", fileName);
  fs_id.add(jsonObject.getString("fs_id"));
  String fileInfo = HttpRequest
    .getData("http://pan.baidu.com/api/sharedownload?sign="
      + sign
      + "&timestamp="
      + time
      + "&bdstoken=c51077ce0e0e313a16066612a13fbcd4&channel=chunlei&clienttype=0&web=1&app_id=250528&encrypt=0&product=share&uk="
      + uk + "&primaryid=" + share_id + "&fid_list=%5B"
      + fs_id.get(i) + "%5D");
  JSONArray jsonArray2 = new JSONArray("[" + fileInfo + "]");
  JSONObject json_data = jsonArray2.getJSONObject(0);
  if (json_data.getString("errno").equals("0")) {
   jsonArray2 = new JSONArray(json_data.getString("list"));
   json_data = jsonArray2.getJSONObject(0);
   map.put("url", json_data.getString("dlink"));
  } else if (json_data.getString("errno").equals("-20")) {
   return null;
   // String getVerCode();
  } else {
   return null;
  }
  list.add(map);
 }
 return list;
}
}

希望本文所述对大家的java程序设计有所帮助。

标签:java,链接
0
投稿

猜你喜欢

  • springmvc限流拦截器的示例代码

    2021-09-08 02:50:55
  • Java常量池知识点总结

    2023-01-09 10:23:09
  • java页面中文乱码的解决办法

    2022-09-01 22:59:20
  • java生成XML的方法

    2023-08-12 11:45:46
  • Mapper批量插入Oracle数据@InsertProvider注解

    2023-02-11 15:13:40
  • SpringBoot实现文件上传与下载功能的示例代码

    2021-11-10 09:15:32
  • SpringBoot2之PUT请求接收不了参数的解决方案

    2023-08-23 01:32:07
  • Java 多线程并发ReentrantLock

    2022-02-23 11:53:23
  • Java设计通用的返回数据格式过程讲解

    2023-11-09 00:16:40
  • Spring Boot加密配置文件特殊内容的示例代码详解

    2023-09-18 08:47:24
  • ElasticSearch查询文档基本操作实例

    2023-11-24 14:20:02
  • SpringBoot异常处理器的使用与添加员工功能实现流程介绍

    2021-10-21 19:24:23
  • 使用Spring自定义实现IOC和依赖注入(注解方式)

    2023-09-16 04:42:35
  • 如何利用java控制鼠标操作一些重复的事情

    2021-11-12 12:54:02
  • 必须要学会的JMM与volatile

    2021-07-30 14:07:53
  • Java SpringBoot启动指定profile的8种方式详解

    2023-07-31 21:48:03
  • SpringBoot整合Shiro两种方式(总结)

    2021-09-08 21:28:24
  • Java中定时器Timer致命缺点案例详解

    2022-08-27 08:38:45
  • Spring Boot整合流控组件Sentinel的场景分析

    2023-06-22 19:27:53
  • MyBatis整合Redis实现二级缓存的示例代码

    2022-02-06 15:41:24
  • asp之家 软件编程 m.aspxhome.com