百度PCS API (REST SDK) Java实现文件遍历
2013-10-28 16:00
399 查看
这个PCS API着实搞了几天,有android的SDK,而我不想使用andriod来做,所以只能使用java来实现REST完成功能,即使用java建立URL连接,发送RESTful请求,解析返回结果,将Json格式使用Gson转换为java对象。
开通PCS API的方法还是参考百度开发者中心官网吧。实现循环遍历某一目录下的所有子目录及文件,还有获取配额、文件目录元数据的功能,部分参考网上资料。
开通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()); } }
相关文章推荐
- java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
- 面试题-程序题-Java递归实现遍历文件目录(所有文件夹和子文件)
- JAVA实现文件遍历
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- JAVA实现文件遍历
- java递归实现文件夹文件的遍历输出
- java resteasy restful webservice教程(六):实现文件下载功能实例文章来源:爱上123 原文地址:http://www.ishang123.com/jishubowen/
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- JAVA基础初探(十二)Map接口及其常用实现类(HashMap)、File类详解(概述、创建、删除、重命名、文件属性读取/设置、遍历文件夹)
- Java 实现 文件目录 遍历 - Java 学习笔记 (22)
- Ueditor结合七牛云及百度云存储(JAVA版,ueditor-1.4.3)实现图片文件上传
- Java实现文件目录的遍历
- java实现遍历文件目录,并在控制台以树形结构展示搜索出来的文件
- Java实现文件的遍历 类型匹配(图形化界面)
- Java遍历文件树的实现和测试
- JAVA实现遍历文件夹下的所有文件(递归调用和非递归调用)
- JAVA文件树遍历
- JAVA实现TCP网络文件复制
- Java 实现自动监听并更新配置文件内容 (转自高手)
- Java实现拷贝文件详细代码