今天上午順利實作了基于tftp進行檔案上傳和下載下傳,下午順利實作了ftp的檔案上傳下載下傳,而且實作了兩種方法
,好happy,當然了,先寫個部落格慶祝一下!這個部落格介紹使用curl實作ftp的方法,下一個介紹使用qFtp插件實作ftp的方法。
1.搭建ftp測試環境
有個比較詳細的文章,圖文并茂,位址為http://jingyan.baidu.com/article/67508eb4d6c4fd9ccb1ce470.html。這裡隻寫操作和步驟。
(1)安裝vsftpd
和tftp一樣,我們首先搭建ftp測試環境,這次我使用的是win7+Ubuntu14虛拟機的方式。首先在vmvare虛拟機中安裝Ubuntu14系統,将網絡調通。接下來要在Ubuntu中安裝 vsftpd,指令為:
sudo apt-get install vsftpd
然後回車,等待安裝完成。測試是否安裝成功的指令為: sudo service vsftpd restart"重新開機vsftpd服務-->回車-->vsftpd處于運作狀态會有下面的輸出:
vsftpd stop/waiting
vsftpd start/running, process 42598
(2)建立使用者
為了便于管理,我們建立一個使用者專門進行ftp功能。 建立"/home/uftp"目錄作為使用者主目錄
打開"終端視窗",輸入"sudo mkdir /home/uftp"-->回車-->輸入"sudo ls /home"-->回車-->有一個uftp目錄,目錄建立成功。
建立使用者uftp并設定密碼
打開"終端視窗",輸入"sudo useradd -d /home/uftp -s /bin/bash uftp"-->回車-->使用者建立成功-->輸入"sudo passwd uftp"設定uftp使用者的密碼-->回車-->輸入兩次密碼-->回車-->密碼設定成功。(假定密碼為123456)
(3)使用gedit修改配置檔案/etc/vsftpd.conf
打開"終端視窗",輸入"sudo gedit /etc/vsftpd.conf"-->回車-->打開了vsftpd.conf檔案,向檔案中添加"userlist_deny=NO
userlist_enable=YES userlist_file=/etc/allowed_users"和"seccomp_sandbox=NO"-->使檔案中的"local_enable=YES"-->儲存。
使用gedit建立/etc/allowed_users檔案
打開"終端視窗",輸入"sudo gedit /etc/allowed_users"-->回車-->輸入uftp-->儲存, 檔案建立成功。
使用gedit檢視/etc/ftpusers檔案中的内容。
打開"終端視窗",輸入"sudo gedit /etc/ftpusers"-->回車-->打開這個檔案後,看一看有沒有uftp這個使用者名,如果沒有,就直接退出。如果有就删除uftp,因為這個檔案中記錄的是不能通路FTP伺服器的使用者清單。
(4)使用winscp登入FTP伺服器
下載下傳安裝WinSCP或者FileZilla等ftp工具,輸入IP、使用者名、密碼-->儲存-->勾選"儲存密碼"-->确定-->登入-->登入成功。
2.解決vsftpd 530 Permission denied 的問題
上面操作完成之後就可以登入并且下載下傳檔案了,但是上傳檔案的時候,有時會遇到“vsftpd 530 Permission denied ”的問題,很明顯這是ftp伺服器拒絕外部的寫入了。網上搜尋了很多,但是效果不好,其實最直接的方法就是修改 "sudo gedit /etc/vsftpd.conf"檔案,将write_enable=YES前面的注釋去掉,然後重新開機vsftpd服務就行了。
3.編寫cur檔案下載下傳的代碼
廢話少說,直接上代碼:其實這個函數和上一篇裡介紹tftp的基本一樣,不同點隻是url位址。其實這也說明了curl的确設計得非常好,不需要我們做太多操作就能很好的完成需要的功能。
struct FtpFile {
const char *filename;
FILE *stream;
};
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream) {
/* open file for writing */
out->stream=fopen(out->filename, "wb");
if(!out->stream)
return -1; /* failure, can't open file to write */
}
qDebug()<<"filename:"<<out->filename;
return fwrite(buffer, size, nmemb, out->stream);
}
int downloadFileFromServer()
{
CURL *curl;
CURLcode res;
struct FtpFile ftpfile={
"D:/tmp/tftpc.c", /* name to store the file as if succesful */
NULL
};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
/*
* You better replace the URL with one that works! Note that we use an
* FTP:// URL with standard explicit FTPS. You can also do FTPS:// URLs if
* you want to do the rarer kind of transfers: implicit.
*/
curl_easy_setopt(curl, CURLOPT_URL,"ftp://uftp:[email protected]/tftps.cpp");
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_FTP_FILEMETHOD, CURLFTPMETHOD_NOCWD);
/* We activate SSL and we require it for both control and data */
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
if(CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}
if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
curl_global_cleanup();
return 0;
}
外部調用接口為:
downloadFileFromServer
4.檔案上傳代碼
檔案上傳的代碼如下所示,其實與tftp的差異也在URL的寫法上。
/*upload*/
#define LOCAL_FILE "D:/tmp/curl.c"
#define UPLOAD_FILE_AS "uploading.txt"
#define REMOTE_URL "ftp://uftp:[email protected]:21/uploading2.txt"
#define RENAME_FILE_TO "renamed.txt"
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
curl_off_t nread;
/* in real-world cases, this would probably get this data differently
as this fread() stuff is exactly what the library already would do
by default internally */
size_t retcode = fread(ptr, size, nmemb, (FILE*)stream);
nread = (curl_off_t)retcode;
fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
" bytes from file\n", nread);
return retcode;
}
int ftpfileUpload()
{
CURL *curl;
CURLcode res;
FILE *hd_src;
struct stat file_info;
curl_off_t fsize;
struct curl_slist *headerlist=NULL;
static const char buf_1 [] = UPLOAD_FILE_AS;
static const char buf_2 [] = RENAME_FILE_TO;
/* get the file size of the local file */
if(stat(LOCAL_FILE, &file_info)) {
printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
return 1;
}
fsize = (curl_off_t)file_info.st_size;
printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
/* get a FILE * of the same file */
hd_src = fopen(LOCAL_FILE, "rb");
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* build a list of commands to pass to libcurl */
headerlist = curl_slist_append(headerlist, buf_1);
headerlist = curl_slist_append(headerlist, buf_2);
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* specify target */
curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
/* pass in that last of FTP commands to run after the transfer */
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
/* Set the size of the file to upload (optional). If you give a *_LARGE
option you MUST make sure that the type of the passed-in argument is a
curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
make sure that to pass in a type 'long' argument. */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)fsize);
/* Now run off and do what you've been told! */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* clean up the FTP commands list */
curl_slist_free_all (headerlist);
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(hd_src); /* close the local file */
curl_global_cleanup();
return 0;
}
外部調用接口為:
ftpfileUpload();
好了,盡情搖擺吧!!!!!!