java上传⽂件到百度⽹盘,使⽤Java语⾔编写的Demo:上传⽂
件⾄百度⽹盘(Baidu。。。
为什么会有这个Demo? 原因⼤致两点:
1. 官⽅提供Android PCS SDK,但有些移动应⽤开发者可能仅仅使⽤⼀⼩部分Baidu PCS的能⼒,希望不链接SDK,⽽⾃⼰实现功能以减少应⽤的尺⼨。
2. 使⽤Java做server端的开发者,显然不能使⽤Android PCS SDK。
技术⽂档:
PCS官⽅API说明
使⽤⽅法:
1. 下载这份PCSUploadDemo.java的代码。
2. 编译它。
(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.*;
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.ity.UrlEncodedFormEntity;
import org.apache.hods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.ity.mime.MultipartEntity;
import org.t.ByteArrayBody;
import org.t.ContentBody;
import org.t.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.ssage.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class PCSUploadDemo {
//post请求地址
private final static String API = "pcs.baidu/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("");
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 = ute(post);
System.out.StatusLine().toString());
System.out.Entity()));
下载apache}
}
// 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 = String(paramsEntity);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return ret;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论