百度PCS API (REST SDK) Java实现文件遍历
来源:互联网 发布:淘宝注册要打电话验证 编辑:程序博客网 时间:2024/05/19 20:42
这个PCS API着实搞了几天,有android的SDK,而我不想使用andriod来做,所以只能使用java来实现REST完成功能,即使用java建立URL连接,发送RESTful请求,解析返回结果,将Json格式使用Gson转换为java对象。
开通PCS API的方法还是参考百度开发者中心官网吧。实现循环遍历某一目录下的所有子目录及文件,还有获取配额、文件目录元数据的功能,部分参考网上资料。
package com.pcstest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import com.google.gson.Gson;import com.google.gson.JsonObject;import com.google.gson.JsonParser;public class PcsUtil{ public static String ACCESS_TOKEN = 使用你PCS的Token; public static URL url = null; /** * 获取配合信息 * @throws IOException */ public static void getPCSInfo() throws IOException { URL u = new URL("https://pcs.baidu.com/rest/2.0/pcs/quota?method=info&access_token="+ ACCESS_TOKEN); URLConnection conn = u.openConnection();// 打开网页链接 // 获取用户云盘信息 String cloudJson = getJsonString(conn).toString(); // 通过gson解析成对象 CloudInfo cloudInfo = new Gson().fromJson(cloudJson, CloudInfo.class); System.out.println("云盘信息:"+cloudInfo); } /** * 获取指定目录元数据信息,URL地址中与配额quota不同,此处为file * @param path * @throws IOException */ public static void getDirMeta(String path) throws IOException { URL u = new URL("https://pcs.baidu.com/rest/2.0/pcs/file?method=meta&access_token=" + ACCESS_TOKEN + "&path=" + path); URLConnection conn = u.openConnection();// 打开网页链接 String meta = getJsonString(conn).toString(); System.out.println("目录信息:"+ meta); } /** * 获取path路径下的文件包括子目录,逐级递归下级目录 * @param path 指定文件路径 ,PCS目录文件结构顶级为固定/apps,下一级为开通PCS API时指定的home目录, * 如我申请时设置的为myhome, 则获取该目录下的所有文件, path为 /apps/myhome * @param fileList 为输出参数, 文件添加到该list中,这里用到了递归 * @throws IOException */ public static void getDirFile(String path, ArrayList<FileInfo> fileList) throws IOException { URL u = new URL("https://pcs.baidu.com/rest/2.0/pcs/file?method=list&access_token=" + ACCESS_TOKEN + "&path=" + path); URLConnection conn = u.openConnection();// 打开网页链接 String str = getJsonString(conn).toString(); JsonParser parser = new JsonParser(); JsonObject jsonObject = (JsonObject) parser.parse(str); FileInfo[] files=new Gson().fromJson(jsonObject.getAsJsonArray("list").toString(), FileInfo[].class); for(FileInfo file: files) { if(file.getIsdir() == 1) { /* 如果是目录,则进行递归查找*/ getDirFile(file.getPath(), fileList); } fileList.add(file); System.out.println("文件路径:"+ file.getPath()); } } private static StringBuffer getJsonString(URLConnection conn) throws IOException { InputStreamReader isr = new InputStreamReader(conn.getInputStream(),"utf-8"); BufferedReader br = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); String line = null; try { while ((line = br.readLine()) != null) { sb.append(line); } } catch (IOException e) { System.out.println("read io error."); e.printStackTrace(); br.close(); isr.close(); } return sb; } }
package com.pcstest;/* 对照REST API返回形式,定义文件对象 */public class FileInfo{ private long fsId; private String path; private int ctime; private int mtime; private String md5; private long size; private int isdir; public long getFsId() { return fsId; } public void setFsId(long fsId) { this.fsId = fsId; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public int getCtime() { return ctime; } public void setCtime(int ctime) { this.ctime = ctime; } public int getMtime() { return mtime; } public void setMtime(int mtime) { this.mtime = mtime; } public String getMd5() { return md5; } public void setMd5(String md5) { this.md5 = md5; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } public int getIsdir() { return isdir; } public void setIsdir(int isdir) { this.isdir = isdir; }}
package com.pcstest;import java.io.IOException;import java.util.ArrayList;public class Main{ public static void main(String[] args) throws IOException { ArrayList<FileInfo> fileList = new ArrayList<FileInfo>(); PcsUtil.getDirFile("/apps/lgodh", fileList); System.out.println("文件个数:"+ fileList.size()); }}
- 百度PCS API (REST SDK) Java实现文件遍历
- 使用HttpClient实现百度PCS文件上传
- 百度 PCS API的使用方法
- 百度网盘API的操作--PCS 百度个人云存储 上传 ,下载文件
- 百度云盘PCS API使用
- 【Java】百度语音识别REST API使用方法不需要集成SDK的方法
- 如何申请使用百度PCS API来让文件上传到云端
- 一个简单的语音识别实现---百度在线语音识别REST API SDK(Python)简单使用
- 使用Java语言编写的Demo:上传文件至百度网盘(Baidu PCS)
- 百度语音识别REST API使用方法(含JAVA代码)——不需要集成SDK的方法
- 【Python】调用百度REST API实现语音识别
- 【Python】调用百度REST API实现语音识别
- 百度PCS 测试案例
- baidu pcs api
- JAVA实现文件遍历
- 百度pcs关闭服务怎么办?
- 用API实现遍历文件目录
- 百度语音识别REST API使用方法(含C++代码)——不需要集成SDK的方法
- android中用Spannable在TextView中设置超链接、颜色、字体等
- 将所配置的初始化参数重置为系统默认值
- 关于c++ 中while(cin>>temp)相关问题搜集的一些知识
- 启动VMware USB Arbitration Service服务时提示:错误31:连接到系统上的设备没有发挥作用
- struts2 注解配置
- 百度PCS API (REST SDK) Java实现文件遍历
- Tomcat负载均衡
- python学习笔记: 一
- python,os操作文件,文件路径(上一级目录)
- 【Hadoop】完全分布式添加新节点
- 用GDB调试core dump文件
- Linux程序前台后台切换
- 模式识别 - "分类" 和 "回归" 的区别
- WIFI模块