不知道有人用过curl写过c的上传程序吗?
我就是按照 liburl里面的例子写的上传文件到http服务器的代码,(有一个 fileupload.c)运行时候就报错,说 0xXXX内存不能为read,NTDLL访问冲突. int main(void)
{
  CURL *curl;
  CURLcode res;
  FILE *fd;  fd = fopen("c:/test.txt", "rb"); /* open file to upload */
  if(!fd) {    return 1; /* can't continue */
  }  curl = curl_easy_init();
  if(curl) {
    /* upload to this place */
    curl_easy_setopt(curl, CURLOPT_URL,
                     "http://localhost:8080/testuploadServlet");    /* tell it to "upload" to the URL */
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);    /* set where to read from (on Windows you need to use READFUNCTION too) */
    curl_easy_setopt(curl, CURLOPT_READDATA, fd); <---加了这句就报错    /* enable verbose for easier tracing */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);    res = curl_easy_perform(curl); // <---------执行到这里出问题!最不好调试!!
       curl_easy_cleanup(curl);
  }
  return 0;
}
加了“curl_easy_setopt(curl, CURLOPT_READDATA, fd)”就报错,如果不加的话,在服务器端居然没有走 multipart这个分支,服务器给当成普通post请求(不是multipart/form-data)处理了!
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
}else{
//走了这里?!!
}详细错误信息: "0x77f92373指令引用的"0x00000010"内存。该内存不能为"written".这种错误应该怎么调试啊?是不是看哪个变量去写0x00000010这块内存了?

解决方案 »

  1.   

    我使用过curl下载文件,很方便
      

  2.   


    BOOL CUpdateManager::DownloadFile(const char* szFileUrl, const char* szFileName)
    {
    // 这里有很多代码我都省略了,下面是调用Curl部分 FILE* pFile = fopen(szFileName, "wb");
    pCurl = curl_easy_init();
    if(pCurl) 
    {
    curl_easy_setopt(pCurl, CURLOPT_URL, szFileUrl); curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, DownloadData);
    curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pFile);
    if (m_hProgress != NULL)
    {
    s_nRatio = 0;
    curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, FALSE);
    curl_easy_setopt(pCurl, CURLOPT_PROGRESSFUNCTION, SetProgress);
    curl_easy_setopt(pCurl, CURLOPT_PROGRESSDATA, m_hProgress);
    }
    //curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, errorBuffer); res = curl_easy_perform(pCurl); curl_easy_cleanup(pCurl);

    if (m_hTip != NULL)
    {
    char szTip[300];
    sprintf(szTip, "下载%s%s", strFileName.c_str(), (res != 0) ? "失败" : "成功");
    ::SetWindowTextA(m_hTip, szTip);
    }
    if (m_hProgress != NULL)
    {
    SetProgress(m_hProgress, 1.0, 1.0, 1.0, 1.0); 
    }
    if (res == 0)
    {
    fflush(pFile);
    fclose(pFile);
    pFile = NULL;
    }
    return (res == 0);
    }
    return FALSE;
    }size_t CUpdateManager::DownloadData(unsigned char *data, size_t size, size_t nmemb, FILE *fp)
    {
    if (s_bAbort || !fp)
    return 0; size_t len = size * nmemb;
    fwrite(data, len, 1, fp);
    return len;
    };
      

  3.   

    所以我估计你需要定义一个读取函数,类似我的DownloadData
      

  4.   

    DownloadData 这个回调函数里面的“unsigned char *data, size_t size, size_t nmemb, FILE *fp” 这些参数是怎么传给它的? 我怎么没看到你传给它这些参数啊?就直接在函数里面使用了?我的问题是上传,能否用我的代码帮忙试验一下? 就是example里面那个 fileupload.c 文件!谢谢
      

  5.   

            curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, DownloadData);
            curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pFile);注意这两个函数,
    curl里面有很多都是回调函数机制,所以这里我传个curl一个回调函数DownloadData, 一个文件指针当curl下载到数据时,curl调用DownloadData这个回调函数,DownloadData的原型是curl规定的,参数也都由curl设置。