當前位置:首頁 » 文件管理 » php百度雲上傳

php百度雲上傳

發布時間: 2024-12-31 15:50:40

⑴ 如何使用百度雲API介面

http://developer..com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E8%B5%84%E6%BA%90%E4%B8%8B%E8%BD%BD
學習了網路雲盤文件API介面的使用;初步是想做一個在線android應用,應用中的文檔是存放在網路雲盤的。
主要是分一下幾個步驟:
1.注冊網路賬號
2.登錄網路開發者中心
3.創建移動應用,獲取對應的(API Key Secret Key)
4.開通pcs API許可權
5.獲取ACCESS_token(認證編碼)
6.開發應用
注意:
開通移動應用,獲取key
獲取token的時候我使用的安卓獲取的方式
通過我寫對應api的例子我發現,其實就兩種情況:一種是get方式提交數據,另外一種是post方式提交數據
1.get方式提交數據,我們用獲取雲盤的信息為例:
獲取雲盤信息前我們要知道,我們要准備好什麼數據:
請求參數:
url: 標明我們要訪問的網址路徑 值固定問""
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("?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("?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: 標明我們要訪問的網址路徑 值固定問""
method:標明我們是請求雲盤信息 值固定為"upload"
acceess_token:准入標識 值是我們自己申請的
path:是我們要上傳到雲盤的那個路徑下 如/apps/myBaiCloud/ myBaiCloud是我們的應用名稱(當你獲取koten後就會自動生成以你應用名稱為名的文件夾
file:這個就是我們要上傳的文件了(要求用post方式上傳)
onp:可選參數,標識當有重名的文件的時候處理方式具體見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%2fmy%2f"; // 我用的是url編碼過源碼為:-> "/apps/my/
/"
//將需要url傳值的參數和url組裝起來
String u =""+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%2fmy%2f"; // 我用的是url編碼過源碼為:-> "/apps/my/
/"
//將需要url傳值的參數和url組裝起來
String u =""+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/my/目錄下找到README.md文件
commons-codec-1.3.jar
commons-
commons-logging.jar
gson-2.2.1.jar
jsoup-1.6.3.jar

⑵ 網盤為什麼禁止上傳asp,aspx,php,jsp,cgi,asa,cer,cdx,hta

asp,aspx,php,jsp,cgi,asa,cer,cdx,hta
看看這類後綴就知道,都是用來寫程序的,這些文件是程序代碼的載體。直接上傳,務必引起安全問題。

所以,此問答的答案解決你兩個問題應該足夠了:
一、不能上傳的原因是,出於安全考慮
二、想上傳,先把這類文件打包(壓縮成.rar等壓縮文件格式)再上傳

熱點內容
掛qphp 發布:2025-03-14 10:13:12 瀏覽:640
資料庫關系表示 發布:2025-03-14 10:13:11 瀏覽:232
腳本抖音號 發布:2025-03-14 10:11:07 瀏覽:668
演算法第 發布:2025-03-14 04:40:56 瀏覽:227
天選2什麼配置好 發布:2025-03-14 03:37:17 瀏覽:287
魅族手機怎麼找回密碼 發布:2025-03-14 02:35:48 瀏覽:298
配置高低主要看什麼 發布:2025-03-14 01:49:22 瀏覽:88
locpython 發布:2025-03-14 01:12:50 瀏覽:352
java數組的定義方法 發布:2025-03-14 00:53:25 瀏覽:527
壓縮性綳帶 發布:2025-03-14 00:30:21 瀏覽:187