您好,欢迎访问一九零五行业门户网

Baidu云盘【文件API】接口学习 『一』

这几天很有兴致的 学习 了百度云盘 文件 api 接口 的使用;初步是想做一个在线android应用,应用中的文档是存放在百度云盘的。 主要是分一下几个步骤: 1.注册百度账号 2.登录百度开发者中心 3.创建移动应用,获取对应的(api key secret key) 4.开通pcs ap
这几天很有兴致的学习了百度云盘文件api接口的使用;初步是想做一个在线android应用,应用中的文档是存放在百度云盘的。
主要是分一下几个步骤:
1.注册百度账号
2.登录百度开发者中心
3.创建移动应用,获取对应的(api key   secret key)
4.开通pcs api权限
5.获取access_token(认证编码)
6.开发应用
注意:
开通移动应用,获取key
获取token的时候我使用的安卓获取的方式
通过我写对应api的例子我发现,其实就两种情况:一种是get方式提交数据,另外一种是post方式提交数据
1.get方式提交数据,我们用获取云盘的信息为例:
获取云盘信息前我们要知道,我们要准备好什么数据:
请求参数:
url: 标明我们要访问的网址路径   值固定问“https://pcs.baidu.com/rest/2.0/pcs/quota”
method:标明我们是请求云盘信息   值固定为“info”
acceess_token:准入标识  值是我们自己申请的
接收返回参数:
        quota:云盘总容量
used:云盘使用容量
request_id:该请求的表示,没啥用
        返回的一个json串如下格式:{quota:123794882560, used:83573494688,request_id:2853739529}
我在做的时候你使用gson工具将json串转换到对应的entity类中了 代码如下:
[html]  /** 
 * @param urlconnection conn通过get方式获取stringbuffer 
 * @return 
 */ 
private stringbuffer getjsonstring(urlconnection conn) { 
    inputstreamreader isr = null; 
    bufferedreader br = null; 
    stringbuffer sb = null; 
    try { 
        isr = new inputstreamreader(conn.getinputstream(),gb2312); 
        br = new bufferedreader(isr); 
        string line = null; 
        sb = new stringbuffer(); 
        while ((line = br.readline()) != null) { 
            sb.append(line); 
            sb.append(\r\n); 
        } 
    } catch (unsupportedencodingexception e) { 
        e.printstacktrace(); 
    } catch (ioexception e) { 
        e.printstacktrace(); 
    }finally{ 
        try { 
            if(isr!=null) 
                isr.close(); 
        } catch (ioexception e) { 
            system.out.println(流关闭是异常); 
            e.printstacktrace(); 
        } 
    }
return sb; 
}
/** 
 * @return 
 * @throws exception 
 *             获取云空间的信息 
 */ 
public cloudinfo getcloudinfo() throws exception { 
    url u = new url(https://pcs.baidu.com/rest/2.0/pcs/quota?method=info&access_token=你申请的token的值; 
    urlconnection conn = u.openconnection();// 打开网页链接 
    // 获取用户云盘信息 
    string cloudjson = this.getjsonstring(conn).tostring();
// 解析成对象 下面有这个实体对象的类 
    gson gson = new gson(); 
    cloudinfo cloudinfo = gson.fromjson(cloudjson, cloudinfo.class); 
    system.out.println(云盘信息:+cloudinfo); 
    return cloudinfo; 

 /**
  * @param urlconnection conn通过get方式获取stringbuffer
  * @return
  */
 private stringbuffer getjsonstring(urlconnection conn) {
  inputstreamreader isr = null;
  bufferedreader br = null;
  stringbuffer sb = null;
  try {
   isr = new inputstreamreader(conn.getinputstream(),gb2312);
   br = new bufferedreader(isr);
   string line = null;
   sb = new stringbuffer();
   while ((line = br.readline()) != null) {
    sb.append(line);
    sb.append(\r\n);
   }
  } catch (unsupportedencodingexception e) {
   e.printstacktrace();
  } catch (ioexception e) {
   e.printstacktrace();
  }finally{
   try {
    if(isr!=null)
     isr.close();
   } catch (ioexception e) {
    system.out.println(流关闭是异常);
    e.printstacktrace();
   }
  }
  return sb;
 }
 /**
  * @return
  * @throws exception
  *             获取云空间的信息
  */
 public cloudinfo getcloudinfo() throws exception {
  url u = new url(https://pcs.baidu.com/rest/2.0/pcs/quota?method=info&access_token=你申请的token的值;
  urlconnection conn = u.openconnection();// 打开网页链接
  // 获取用户云盘信息
  string cloudjson = this.getjsonstring(conn).tostring();
  // 解析成对象 下面有这个实体对象的类
  gson gson = new gson();
  cloudinfo cloudinfo = gson.fromjson(cloudjson, cloudinfo.class);
  system.out.println(云盘信息:+cloudinfo);
  return cloudinfo;
 }
[html] package com.entity;
import java.lang.reflect.type;
/** 
 * @author ydcun 获取云空间的信息 例如: 
 *              {quota:123794882560, 空间配额,单位为字节 
 *               used:83573494688, 已使用空间大小 单位为字节。 
 *               request_id:2853739529} 
 */ 
public class cloudinfo{ 
    private double quota; 
    private double used; 
    private double request_id; 
    /** 
     * @return the quota  空间配额,单位为字节 
     */ 
    public double getquota() { 
        return quota; 
    } 
    /** 
     * @param quota the quota to set  空间配额,单位为字节 
     */ 
    public void setquota(double quota) { 
        this.quota = quota; 
    } 
    /** 
     * @return the used 已使用空间大小 单位为字节 
     */ 
    public double getused() { 
        return used; 
    } 
    /** 
     * @param used the used to set 已使用空间大小 单位为字节 
     */ 
    public void setused(double used) { 
        this.used = used; 
    } 
    /** 
     * @return the request_id  
     */ 
    public double getrequest_id() { 
        return request_id; 
    } 
    /** 
     * @param request_id the request_id to set 
     */ 
    public void setrequest_id(double request_id) { 
        this.request_id = request_id; 
    } 
    @override 
    public string tostring() {
return new stringbuffer().append(空间容量:).append(this.getquota()/1024/1024).append(m;  已用:).append(this.getused()/1024/1024).append(m; ).tostring(); 
    } 

package com.entity;
import java.lang.reflect.type;
/**
 * @author ydcun 获取云空间的信息 例如:
 *     {quota:123794882560, 空间配额,单位为字节
 *           used:83573494688, 已使用空间大小 单位为字节。
 *            request_id:2853739529}
 */
public class cloudinfo{
 private double quota;
 private double used;
 private double request_id;
 /**
  * @return the quota  空间配额,单位为字节
  */
 public double getquota() {
  return quota;
 }
 /**
  * @param quota the quota to set  空间配额,单位为字节
  */
 public void setquota(double quota) {
  this.quota = quota;
 }
 /**
  * @return the used 已使用空间大小 单位为字节
  */
 public double getused() {
  return used;
 }
 /**
  * @param used the used to set 已使用空间大小 单位为字节
  */
 public void setused(double used) {
  this.used = used;
 }
 /**
  * @return the request_id
  */
 public double getrequest_id() {
  return request_id;
 }
 /**
  * @param request_id the request_id to set
  */
 public void setrequest_id(double request_id) {
  this.request_id = request_id;
 }
 @override
 public string tostring() {
return new stringbuffer().append(空间容量:).append(this.getquota()/1024/1024).append(m;  已用:).append(this.getused()/1024/1024).append(m; ).tostring();
 }
}
2.通过post方式提交  我用上传单个文件为例子:
同样我们也先了解下上传文件要参数设置:
请求参数:
url: 标明我们要访问的网址路径   值固定问“https://pcs.baidu.com/rest/2.0/pcs/file”
method:标明我们是请求云盘信息   值固定为“upload”
acceess_token:准入标识  值是我们自己申请的
path:是我们要上传到云盘的那个路径下     如/apps/mybaiducloud/        mybaiducloud是我们的应用名称(当你获取koten后就会自动生成以你应用名称为名的文件夹)
file:这个就是我们要上传的文件了(要求用post方式上传)
ondup:可选参数,标识当有重名的文件的时候处理方式具体见api
接收返回参数:
       返回的也是json串,
path:为我们上传的文件保存的全路径
size:文件的大小有多少字节
ctime/mtime:文件的创建修改时间
其他参数介绍点小标题去api中查看
{
     path : /apps/album/readme.md
     size : 372121,
     ctime : 1234567890,
     mtime : 1234567890,
     md5 : cb123afcc12453543ef,
     fs_id : 12345,
    request_id:4043312669
}
我在做的时候也是将其封装到实体类中了,这里和上面一样不详述,我们重点看下提交文件是怎么提交的代码如下:
[java] /**
     * @param path 云盘存放路径
     * @param name 要上传的文件
     * @return
     * @throws exception 
     */ 
    public filebase uploadfile(string path,file file) throws exception{ 
        //模拟文件  
        string filename=readme.md; 
        file = new file(filename); 
        path=%2fapps%2fmybaidu%2f;    //  我用的是url编码过源码为:->  /apps/mybaidu/  
/
//将需要url传值的参数和url组装起来  
        string u =https://pcs.baidu.com/rest/2.0/pcs/file?path=+path+file.getname()+&method=upload&access_token=你自己申请的token值;
postmethod filepost = new postmethod(u); 
        //post提交的参数  
        part[] parts = {new filepart(filename,file)}; 
        //设置多媒体参数,作用类似form表单中的enctype=multipart/form-data   
        filepost.setrequestentity(new multipartrequestentity(parts, filepost.getparams())); 
        httpclient clients = new httpclient(); 
        //响应代码  
        int status = clients.executemethod(filepost); 
        system.out.println(成功上传+path+filename);
bufferedreader bureader = new bufferedreader(new inputstreamreader(filepost.getresponsebodyasstream(),utf-8)); 
        stringbuffer sb = new stringbuffer(); 
        string line; 
        while((line=bureader.readline())!=null){ 
            sb.append(line); 
        } 
        bureader.close();
// 解析成对象  
        gson gson = new gson(); 
        filebase cloudinfo = gson.fromjson(sb.tostring(), filebase.class);
return cloudinfo; 
    } 
/**
  * @param path 云盘存放路径
  * @param name 要上传的文件
  * @return
  * @throws exception
  */
 public filebase uploadfile(string path,file file) throws exception{
  //模拟文件
  string filename=readme.md;
  file = new file(filename);
  path=%2fapps%2fmybaidu%2f;    //  我用的是url编码过源码为:->  /apps/mybaidu/
/
//将需要url传值的参数和url组装起来
  string u =https://pcs.baidu.com/rest/2.0/pcs/file?path=+path+file.getname()+&method=upload&access_token=你自己申请的token值;
postmethod filepost = new postmethod(u);
  //post提交的参数
  part[] parts = {new filepart(filename,file)};
  //设置多媒体参数,作用类似form表单中的enctype=multipart/form-data
  filepost.setrequestentity(new multipartrequestentity(parts, filepost.getparams()));
  httpclient clients = new httpclient();
  //响应代码
  int status = clients.executemethod(filepost);
  system.out.println(成功上传+path+filename);
bufferedreader bureader = new bufferedreader(new inputstreamreader(filepost.getresponsebodyasstream(),utf-8));
  stringbuffer sb = new stringbuffer();
  string line;
  while((line=bureader.readline())!=null){
   sb.append(line);
  }
  bureader.close();
// 解析成对象
  gson gson = new gson();
  filebase cloudinfo = gson.fromjson(sb.tostring(), filebase.class);
return cloudinfo;
 }
上面代码成功后我们就会在/apps/mybaidu/目录下找到readme.md文件
上面代码执行还要倒入对应的jar包:下载
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging.jar
gson-2.2.1.jar
jsoup-1.6.3.jar
其他的api怎么用百度给了一个很好的演示平台:
其它类似信息

推荐信息