百度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());    }}