用过libcurl上传文件的朋友帮忙!解决方法
用过libcurl上传文件的朋友帮忙!
现在利用libcurl库上传文件,结果老显示302 moved temporarily
上传不上去,咋回事啊?
curl = curl_easy_init();
if (curl != NULL) {
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.119:18080/data ");
stat( "tt.txt ",&file_info);
fp = fopen( "tt.txt ", "r ");
if (fp == NULL) {
curl_easy_cleanup(curl);
return -1;
}
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_READDATA, fp);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_perform(curl);
fclose(fp);
fp = NULL;
curl_easy_cleanup(curl);
------解决方案--------------------
我找的例子:
FILE *f;
long uploaded_len = 0;
CURLcode r = CURLE_GOT_NOTHING;
int c;
f = fopen(localpath, "rb ");
if (f == NULL) {
perror(NULL);
return 0;
}
curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, TRUE);
curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
if (timeout)
curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "- "); /* disable passive mode */
curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, TRUE);
curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, TRUE);
for (c = 0; (r != CURLE_OK) && (c < tries); c++) {
/* are we resuming? */
if (c) { /* yes */
/* determine the length of the file already written */
/*
* With NOBODY and NOHEADER, libcurl will issue a SIZE
* command, but the only way to retrieve the result is
* to parse the returned Content-Length header. Thus,
* getcontentlengthfunc(). We need discardfunc() above
* because HEADER will dump the headers to stdout
* without it.
*/
curl_easy_setopt(curlhandle, CURLOPT_NOBODY, TRUE);
curl_easy_setopt(curlhandle, CURLOPT_HEADER, TRUE);
r = curl_easy_perform(curlhandle);
if (r != CURLE_OK)
continue;
curl_easy_setopt(curlhandle, CURLOPT_NOBODY, FALSE);
curl_easy_setopt(curlhandle, CURLOPT_HEADER, FALSE);
fseek(f, uploaded_len, SEEK_SET);
curl_easy_setopt(curlhandle, CURLOPT_FTPAPPEND, TRUE);
}
else { /* no */
curl_easy_setopt(curlhandle, CURLOPT_FTPAPPEND, FALSE);
}
r = curl_easy_perform(curlhandle);
}
fclose(f);
if (r == CURLE_OK)
return 1;
else {
fprintf(stderr, "%s\n ", curl_easy_strerror(r));
return 0;
}
没有续传,高手指点怎么玩!
------解决方案--------------------
302move了你就要向move后的地址发呀
现在利用libcurl库上传文件,结果老显示302 moved temporarily
上传不上去,咋回事啊?
curl = curl_easy_init();
if (curl != NULL) {
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.119:18080/data ");
stat( "tt.txt ",&file_info);
fp = fopen( "tt.txt ", "r ");
if (fp == NULL) {
curl_easy_cleanup(curl);
return -1;
}
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_READDATA, fp);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_perform(curl);
fclose(fp);
fp = NULL;
curl_easy_cleanup(curl);
------解决方案--------------------
我找的例子:
FILE *f;
long uploaded_len = 0;
CURLcode r = CURLE_GOT_NOTHING;
int c;
f = fopen(localpath, "rb ");
if (f == NULL) {
perror(NULL);
return 0;
}
curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, TRUE);
curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
if (timeout)
curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "- "); /* disable passive mode */
curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, TRUE);
curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, TRUE);
for (c = 0; (r != CURLE_OK) && (c < tries); c++) {
/* are we resuming? */
if (c) { /* yes */
/* determine the length of the file already written */
/*
* With NOBODY and NOHEADER, libcurl will issue a SIZE
* command, but the only way to retrieve the result is
* to parse the returned Content-Length header. Thus,
* getcontentlengthfunc(). We need discardfunc() above
* because HEADER will dump the headers to stdout
* without it.
*/
curl_easy_setopt(curlhandle, CURLOPT_NOBODY, TRUE);
curl_easy_setopt(curlhandle, CURLOPT_HEADER, TRUE);
r = curl_easy_perform(curlhandle);
if (r != CURLE_OK)
continue;
curl_easy_setopt(curlhandle, CURLOPT_NOBODY, FALSE);
curl_easy_setopt(curlhandle, CURLOPT_HEADER, FALSE);
fseek(f, uploaded_len, SEEK_SET);
curl_easy_setopt(curlhandle, CURLOPT_FTPAPPEND, TRUE);
}
else { /* no */
curl_easy_setopt(curlhandle, CURLOPT_FTPAPPEND, FALSE);
}
r = curl_easy_perform(curlhandle);
}
fclose(f);
if (r == CURLE_OK)
return 1;
else {
fprintf(stderr, "%s\n ", curl_easy_strerror(r));
return 0;
}
没有续传,高手指点怎么玩!
------解决方案--------------------
302move了你就要向move后的地址发呀