c++ 通过curl 向本机apache服务器 上传文件 服务器收不到

sdzangxin123 2012-11-15 12:38:09
代码如下
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

求大神指点 该怎么才能吧 客户端的资源文件发送到服务器那里?
...全文
434 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdzangxin123 2012-12-07
  • 打赏
  • 举报
回复
3L 这就是curl例子的改版。基本没变啊。 怎么设置apache允许上传? 求指点
mymtom 2012-11-30
  • 打赏
  • 举报
回复
楼主的apache服务器不允许上传啊。
mymtom 2012-11-30
  • 打赏
  • 举报
回复
apache服务器不允许上传啊。
pengyw 2012-11-27
  • 打赏
  • 举报
回复
CURL有例子的,自己看看了
sdzangxin123 2012-11-15
  • 打赏
  • 举报
回复
自己顶啊 。求大神指点啊、。感激不尽
转角天边 2012-11-15
  • 打赏
  • 举报
回复
传输文件可以用transmitfile函数 功能说明:TransmitFile 是一个扩展的 API,它允许在套接字连接上发送一个打开的文件。这使得应用程序可以避免亲自打开文件,重复地在文件执行读入操作,再将读入的那块数据写入套接字。相反,已打开的文件的句柄是各套接字连接一起给出的,在套接字上,文件数据的读入和发送都在核心模式下进行。这就避免了亲自执行文件读入时必须的多重内核变换。

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧