为什么会有这个Demo? 原因大致两点:
1. 官方提供Android PCS SDK,但有些移动应用开发者可能仅仅使用一小部分Baidu PCS的能力,希望不链接SDK,而自己实现功能以减少应用的尺寸。
2. 使用Java做server端的开发者,显然不能使用Android PCS SDK。
技术文档:
使用方法:
1. 下载这份PCSUploadDemo.java的代码。
2. 编译它。
(1)这里用到Apache Http的Java库,所以先下载它:http://hc.apache.org/downloads.cgi 。把那些jar库文件准备好。
(2)编译,需要设定CLASSPATH:
oliverluan@YanqiangtekiMacBook-Pro:~/Documents/EvWork/PCSUploadDemo$ javac -cp "httpcore-4.3.2.jar:httpclient-4.3.3.jar:httpmime-4.3.3.jar:./" PCSUploadDemo.java
3. 运行:
oliverluan@localhost:~/Documents/EvWork/PCSUploadDemo$ java -cp "httpcore-4.3.2.jar:httpclient-4.3.3.jar:httpmime-4.3.3.jar:./:commons-logging-1.1.3.jar" PCSUploadDemo
Usage: PCSUploadDemo file_to_upload destination your_access_token
源代码:PCSUploadDemo.java
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class PCSUploadDemo {
//post请求地址
private final static String API = "https://pcs.baidu.com/rest/2.0/pcs/file";
//本地文件路径, 例如:"/Users/macuser/Documents/workspace/test.jpg"
private static String mLocalPath;
//上传文件路径(含上传的文件名称), 例如:"/apps/yuantest/test.jpg"
private static String mDestPath;
//开发者准入标识 access_token, 通过OAuth获得
private static String mAccessToken;
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: PCSUploadDemo file_to_upload destination your_access_token");
return;
}
mLocalPath = args[0];
mDestPath = args[1];
mAccessToken = args[2];
File fileToUpload = new File(mLocalPath);
if (!(fileToUpload).isFile()) {
System.out.println("Input file_to_upload is invalid!");
return;
}
System.out.println("Uploading...");
Thread workerThread = new Thread(new Runnable() {
public void run() {
try {
doUpload();
} catch (IOException e) {
e.printStackTrace();
}
}
});
workerThread.start();
return ;
}
public static void doUpload() throws IOException{
File fileToUpload = new File(mLocalPath);
if(null != fileToUpload && fileToUpload.length() > 0){
ArrayList
params = new ArrayList
();
params.add(new BasicNameValuePair("method", "upload"));
params.add(new BasicNameValuePair("access_token", mAccessToken));
params.add(new BasicNameValuePair("path", mDestPath));
//添加请求参数,通过POST表单进行传递,除上传文件内容之外的其它参数通过query_string进行传递。
String postUrl = API + "?" + buildParams(params);
HttpPost post = new HttpPost(postUrl);
//添加文件内容,必须使用multipart/form-data编码的HTTP entity
MultipartEntity entity = new MultipartEntity();
FileBody fo = new FileBody(fileToUpload);
entity.addPart("uploaded",fo);
post.setEntity(entity);
//创建client
HttpClient client = new DefaultHttpClient();
//发送请求
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine().toString());
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
// url encoded query string
protected static String buildParams(List
urlParams){
String ret = null;
if(null != urlParams && urlParams.size() > 0){
try {
// 指定HTTP.UTF_8为charset参数以保证中文文件路径的正确
HttpEntity paramsEntity = new UrlEncodedFormEntity(urlParams, HTTP.UTF_8);
ret = EntityUtils.toString(paramsEntity);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return ret;
}
}