- 登录网页端获取cookie 金山文档
- maven
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.9</version>
</dependency>
- 代码
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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);}}}
}