这几天很有兴致的学习了百度云盘文件API接口的使用;初步是想做一个在线android应用,应用中的文档是存放在百度云盘的。
主要是分一下几个步骤:
1.注册百度账号
2.登录百度开发者中心
3.创建移动应用,获取对应的(API Key Secret Key)
4.开通pcs API权限
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类中了 代码如下:
- /**
- * @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; }
- 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
}
我在做的时候也是将其封装到实体类中了,这里和上面一样不详述,我们重点看下提交文件是怎么提交的代码如下:
- /**
- * @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怎么用百度给了一个很好的演示平台:
好了剩下的就大家自己去模式吧!
用什么问题联系我我们一起探讨----ydcun
相关推荐
百度云盘迁移阿里云盘文件
为什么360浏览器下载百度云盘文件总是失败.docx
易语言百度云盘解析源码,百度云盘解析,交互日志回调函数,百度网盘地址解析,取文本中间内容,彗星_URL解码_UTF8,彗星_URL编码_UTF8,Ansi转Utf8,Utf8转ansi,Unicode转Utf8,Ansi转Unicode,Unicode转ansi,Utf8转Unicode,...
ppt素材模板(百度云盘)
云盘系统sql文件,提供云盘系统需要的各个表的字段以及少量的数据。
基于阿里网盘api二次开发,得益于阿里网盘的不限速容量大,灵感来自于onedrive...使用JAVA重写项目,基于阿里云盘实现,实现秒传,支持本地更新文件同步到网盘,支持记住登录,扫描登录,支持文件加密和解密 安全性更高
python百度网盘纯代码分片上传目录文件 不依赖第三方, AppKey等信息需要自己去申请。然后获取code码就可以一直使用。
亲测可用 找到百度网盘文件 生成目录txt 复制到excel 编辑保存
百度个人云盘空间大,完全免费,而且提供了pcs api供调用操作文件,在平时的项目里往里面保存一些文件是很实用的。 环境准备: 开通读写网盘的权限及获取access_token:...
JAVA 入门视频教程,java开发视频教程自学就业班送项目实战 百度云盘永久分享链接
其他主题自动化发布阿里云盘文件,百度网盘文件,思路都是一致的。 以代码文件提供,非UI界面; 本脚本含以下功能: 1)使用RestFulAPI自动发布WP文章; 2)自动重命名阿里云盘文件,增加后缀,移动目录; 3)自动...
怎样把百度云盘中文件转存到华为网盘.docx
是比较老的sap系统 适合新手顾问安装 没有数据源 需要自己实施 通过百度云盘分享 如果过期 请告知 联系方式见昵称
java做的仿百度云盘项目,包括上传下载,云盘空间展示等功能。项目封装了很多使用的utils,拿来即用。
Linux百度云盘。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。...
百度云盘离线下载文件.docx
可以卸载掉百度云盘在资源管理器的图标,通过写改注册表。
txt文件里面有百度云盘下载地址,这个是oracle19c客户端,windows64位系统直接双击setup.exe安装即可
一款开源、轻巧且功用齐全的跨平台网盘程序:蓝眼云盘努力于打形成一个文雅,好用,高效,抑制的开源云盘。 蓝眼云盘采用前后端别离的形式开发。后端运用Golang言语,前端采用React框架;前端打包后的静态文件夹...
分布式云盘系统准校园云盘文件管理服务 1、专业的教育资源管理...学校有服务器是我们的客户,没有服务器可以帮客户采购新服务器,育网云盘是一套精准校园文件管理系统,它填补国内教育信息化资源应用领域的一项空白。