c++ 通过curl 向本机apache服务器 上传文件 服务器收不到
代码如下
FILE *fp;
size_t read_data(void *buffer, size_t size, size_t nmemb, void *user_p)
{
return fread(buffer, size, nmemb, fp);
}
int main(int argc, char **argv)
{ // 初始化libcurl CURLcode code;
CURLcode code = curl_global_init(CURL_GLOBAL_WIN32);
if (code != CURLE_OK)
{
cerr << "init libcurl failed." << endl;
return -1;
}
fp = fopen("aaa.txt", "rb");
if (NULL == fp)
{
cout << "can't open file." << endl;
curl_global_cleanup();
return -1;
}
//fseek(fp, 0L, SEEK_END);
//size_t file_size = ::ftell(fp);
//fseek(fp, 0L, SEEK_SET);
// 获取文件大小
fseek(fp, 0, SEEK_END);
int file_size = ftell(fp);
rewind(fp);
// 获取easy handle
CURL *easy_handle = NULL;
easy_handle = curl_easy_init();
if (NULL == easy_handle)
{
cerr << "get a easy handle failed." << endl; fclose(fp);
curl_global_cleanup();
return -1;
}
// 设置eash handle属性
curl_easy_setop(easy_handle,CURLOPT_URL, "http://127.0.0.1/voice/aaa.txt");
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(easy_handle, CURLOPT_READFUNCTION, &read_data);
curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
curl_easy_setopt(easy_handle, CURLOPT_INFILESIZE_LARGE, file_size);
// 执行上传操作
code = curl_easy_perform(easy_handle);
if (code == CURLE_OK)
{
cout << "upload successfully." << endl;
}
// 释放资源
fclose(fp);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
system("pause");
return 0;
}
运行提示错误
Method not allowed!
The PUT method is not allowed for the requested URL
求大神指点 该怎么才能吧 客户端的资源文件发送到服务器那里?