Java和**VC++**调用百度网盘API实现文件上传和下载的最简示例(核心思路,适合入门和集成)
Java和**VC++**调用百度网盘API实现文件上传和下载的最简示例(核心思路,适合入门和集成)一、Java 示例(使用 OkHttp)
需先获取百度网盘开放平台的 access_token(官方文档)
import okhttp3.*;
import java.io.File;
import java.io.IOException;
public class BaiduPanDemo {
// 替换为你的 access_token
static final String ACCESS_TOKEN = "your_access_token";
static final String UPLOAD_URL = "https://pan.baidu.com/rest/2.0/xpan/file?method=upload&access_token=" + ACCESS_TOKEN;
static final String DOWNLOAD_URL = "https://d.pcs.baidu.com/rest/2.0/pcs/file?method=download&access_token=" + ACCESS_TOKEN;
// 上传文件
public static void uploadFile(String localPath, String remotePath) throws IOException {
OkHttpClient client = new OkHttpClient();
File file = new File(localPath);
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("path", remotePath)
.addFormDataPart("file", file.getName(), RequestBody.create(file, MediaType.parse("application/octet-stream")))
.build();
Request request = new Request.Builder()
.url(UPLOAD_URL)
.post(body)
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
// 下载文件
public static void downloadFile(String remotePath, String savePath) throws IOException {
OkHttpClient client = new OkHttpClient();
HttpUrl url = HttpUrl.parse(DOWNLOAD_URL).newBuilder()
.addQueryParameter("path", remotePath)
.build();
Request request = new Request.Builder()
.url(url)
.get()
.build();
Response response = client.newCall(request).execute();
java.nio.file.Files.write(new File(savePath).toPath(), response.body().bytes());
System.out.println("下载完成: " + savePath);
}
public static void main(String[] args) throws IOException {
uploadFile("C:/test.txt", "/apps/yourapp/test.txt");
downloadFile("/apps/yourapp/test.txt", "C:/downloaded_test.txt");
}
}二、VC++ 示例(WinINet)
需先获取 access_token,建议用 HTTPS 库如 WinINet、libcurl 或 QNetworkAccessManager(Qt)
// ...existing code...
#include <windows.h>
#include <wininet.h>
#include <fstream>
#pragma comment(lib, "wininet.lib")
// 上传文件到百度网盘
bool BaiduPanUpload(const CString& localPath, const CString& remotePath, const CString& accessToken)
{
// 构造URL
CString url;
url.Format(_T("https://pan.baidu.com/rest/2.0/xpan/file?method=upload&access_token=%s"), accessToken);
// 读取本地文件
std::ifstream file(localPath, std::ios::binary);
if (!file.is_open()) return false;
std::string fileData((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
// 构造POST数据(multipart/form-data略,实际需用 boundary 格式)
// 这里只做演示,建议用 libcurl 或 C++ REST SDK 实现完整 multipart
CStringA postData;
postData.Format("path=%s&file=%s", CStringA(remotePath), CStringA(fileData.c_str()));
HINTERNET hInet = InternetOpen(_T("BaiduPanDemo"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConn = InternetOpenUrl(hInet, url, NULL, 0, INTERNET_FLAG_SECURE, 0);
DWORD dwWritten = 0;
BOOL bRet = HttpSendRequestA(hConn, "Content-Type: application/x-www-form-urlencoded", -1, (LPVOID)(LPCSTR)postData, postData.GetLength());
// 检查返回
// ...读取返回内容...
InternetCloseHandle(hConn);
InternetCloseHandle(hInet);
return bRet == TRUE;
}
// 下载文件
bool BaiduPanDownload(const CString& remotePath, const CString& savePath, const CString& accessToken)
{
CString url;
url.Format(_T("https://d.pcs.baidu.com/rest/2.0/pcs/file?method=download&access_token=%s&path=%s"), accessToken, remotePath);
HINTERNET hInet = InternetOpen(_T("BaiduPanDemo"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConn = InternetOpenUrl(hInet, url, NULL, 0, INTERNET_FLAG_SECURE, 0);
if (!hConn) { InternetCloseHandle(hInet); return false; }
std::ofstream outFile(savePath, std::ios::binary);
char buffer;
DWORD dwRead = 0;
while (InternetReadFile(hConn, buffer, sizeof(buffer), &dwRead) && dwRead > 0) {
outFile.write(buffer, dwRead);
}
outFile.close();
InternetCloseHandle(hConn);
InternetCloseHandle(hInet);
return true;
}
// ...existing code...注意事项:
需先在百度开放平台申请应用并获取 access_token。
上传接口需用 multipart/form-data,VC++建议用 libcurl 或 C++ REST SDK 实现更完整的 HTTP POST。
真实项目需处理错误、token过期、文件大小限制等。
如需更详细的接口参数和返回解析,请参考百度网盘开放平台文档。
页:
[1]