金山文档批量下载-Java

  1. 登录网页端获取cookie 金山文档
    在这里插入图片描述
  2. maven
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.9</version>
</dependency>
  1. 代码
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author ohy
 * @date 2021-08-26 09:59:38
 **/
public class Test {

    static String cookie ="";

    public static void main(String[] args) {
        String url = "https://drive.kdocs.cn/api/v5/links?offset=0&count=100&orderby=file_mtime&order=DESC&append=false&ignore=link";
        String res = HttpRequest.get(url).header("cookie", cookie).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(res);
        String path = "C:\\Users\\montnets\\Desktop\\test\\"
                + jsonObject.getJSONArray("share").getJSONObject(0).getString("share_name");
        parseGroup(jsonObject.getJSONArray("share").getJSONObject(0).getJSONObject("group").getString("groupid"), path, "0");
    }

    private static void parseGroup(String id, String path, String parentId) {
        String url = "https://drive.kdocs.cn/api/v5/groups/" + id + "/files?linkgroup=true&include=acl,pic_thumbnail&offset=0&count=30";
        if (!parentId.equals("0")) url += "&parentid=" + parentId;
        String res = HttpRequest.get(url).header("cookie", cookie).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(res);
        JSONArray array = jsonObject.getJSONArray("files");
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = array.getJSONObject(i);
            if (object.getString("ftype").equals("folder")) {
                String groupId = object.getString("id");
                String name = object.getString("fname");
                parseGroup(id, path + "\\" + name, groupId);
            } else if (object.getString("ftype").equals("file")) {
                String fId = object.getString("id");
                String name = object.getString("fname");
                String getdownUrl = "https://drive.kdocs.cn/api/v3/groups/" + id + "/files/" + fId + "/download?isblocks=false";
                res = HttpRequest.get(getdownUrl).header("cookie", cookie).execute().body();
                String downUrl = JSONObject.parseObject(res).getJSONObject("fileinfo").getString("url");
                HttpUtil.downloadFile(downUrl, path + "/" + name);
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值