欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 18:01:49
本文实例讲述了java获取百度网盘真实下载链接的方法。分享给大家供大家参考。具体如下: 目前还存在一个问题,同一ip在获取3次以后会出现验证码,会获取失败,感兴趣的朋友对...

本文实例讲述了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程序设计有所帮助。