QT實現(xiàn)FTP上傳文件
本文實例為大家分享了QT實現(xiàn)FTP上傳文件的具體代碼,供大家參考,具體內容如下
兩臺電腦通過網線建立本地連接,保證網關在同一段;
服務器端打開ftp;
控制面板→程序→啟用或關閉windows功能→windows功能→Internet信息服務
啟用“FTP服務”FTP擴展性” IIS管理控制臺”
開始屏幕的搜索中輸入“IIS”,然后點擊打開“IIS管理器”
打開“IIS管理器”后,在左欄的“網站”上點擊右鍵,打開“添加FTP站點”
然后按照提示填寫站點信息
點擊“下一步”,按照下圖提示,設置“綁定和SSL設置”,在“IP地址”處,可以用內網IP也可以用外網IP,訪客自然也就根據你的IP設定來決定;
點擊“下一步”,設置“身份驗證和授權信息”
然后在本機瀏覽器地址欄中輸入“ftp://填寫的IP”測試一下
客戶端網頁測試遠程訪問;
客戶端(另一臺電腦)瀏覽器地址欄中輸入“ftp://填寫的IP”測試一下
客戶端cmd測試遠程訪問;
win+r打開運行窗口,輸入cmd
回車打開cmd命令窗口
cmd命令中輸入:ftp回車
回車切換至ftp命令窗口,輸入命令:open,回車提示:到
到即所要連接測試的ftp地址,我們輸入:IP 22
即:ip地址+空格+端口號,沒有+號
回車后彈出用戶一行,輸入ftp的用戶名后回車,輸入ftp用戶名對應的密碼
輸入密碼后回車,如果提示,user logged in就說么ftp創(chuàng)建無問題
客戶端程序測試遠程訪問。
新建程序,添加ftpclass.cpp、ftpclass.h,復制main.cpp內容到程序入口函數(shù)
注意:/項目-屬性-常規(guī)-字符集-使用多字節(jié)字符集/
//main.cpp #include "stdafx.h" #include "ftpclass.h" void main() { ?? ?printf("------- 開始測試!------\n"); ?? ?printf("01--創(chuàng)建連接 %d\n", FtpClass::createConnection()); ?? ?printf("02--打開目標ftp %d\n", FtpClass::createTable());?? ? ?? ? ?? ?/*可以讀取ini內參數(shù) ?? ?FtpClass::ftp_Ip = TEXT("Ini讀取"); ?? ?FtpClass::ftp_Port = TEXT("Ini讀取"); ?? ?FtpClass::ftp_User = TEXT("Ini讀取"); ?? ?FtpClass::ftp_Password = TEXT("Ini讀取"); ?? ?FtpClass::ftp_Fixed_Path = TEXT("Ini讀取");*/ ?? ? ?? ?printf("03--創(chuàng)建文件夾 %d\n", FtpClass::createFolder("自動生成目錄1","自動生成目錄2","自動生成目錄3"));?? ? ?? ?/*上傳目標路徑*/ ?? ?printf("04--上傳文件 %d\n", FtpClass::insert( "D:/a.txt", "b.txt")); ?? ?/*本機文件需要上傳文件*/ ?/*上傳后文件名稱,可以和本地文件名稱不一樣,類型最好別換*/ ?? ? ?? ?printf("05--關閉通訊 %d\n", FtpClass::createClose()); ?? ?printf("------ 結束測試!------\n"); ?? ? ?? ?return ; }
//ftpclass.h /*項目-屬性-常規(guī)-字符集-使用多字節(jié)字符集*/ /*wininet.lib、shlwapi.lib可以直接添加到附加依賴項*/ /*BOOL_VERIFY、NULL_VERIFY 程序結束判斷*/ #pragma once #pragma comment(lib,"wininet.lib") #pragma comment(lib,"shlwapi.lib") #define ?BOOL_VERIFY(emStatus_bool,switch_bool) \ if (emStatus_bool == true)\ {return true;}\ else{\ if (switch_bool == 3) printf(" ? ? ?FTP_03_err:創(chuàng)建文件夾失?。?d\n"); \ if (switch_bool == 4) printf(" ? ? ?FTP_04_err:上傳文件失?。n"); ? ? \ if (switch_bool == 5) printf(" ? ? ?FTP_05_err:關閉窗口句柄失??!\n"); \ return false;\ } #define ?NULL_VERIFY(emStatus_null,switch_null) \ if (emStatus_null != NULL)\ {return true;}\ else{\ if (switch_null == 1) {printf(" ? ? ?FTP_01_err:打開通訊錯誤 Error:%d\n", GetLastError());}\ if (switch_null == 2) {printf(" ? ? ?FTP_02_err:建立連接錯誤 Error:%d\n", GetLastError());}\ return false;\ } #include "stdafx.h"http://沒用 #include <afxinet.h>//MFC相關 #include "wininet.h"http://調用FTP相關類 #include "shlwapi.h"http://調用文件操作相關類 class FtpClass { public:?? ? ?? ?/*ini讀取變量*/ ?? ?static CString ftp_Ip;//目標ip ?? ?static CString ftp_Port;//目標端口 ?? ?static CString ftp_User;//目標賬戶 ?? ?static CString ftp_Password;//目標密碼 ?? ?static CString ftp_Fixed_Path;//目標固定路徑 ?? ?static CString ftp_Free_Path;//目標自己生成路徑 ?? ? ?? ? /*全局變量*/ ?? ?static BOOL ?pRes; ?? ?static HINTERNET hInternet; ?? ?static HINTERNET hConnect; ?? ? ?? ?/*全局函數(shù)*/ ?? ?static bool createConnection();?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?//創(chuàng)建一個連接 ?? ?static bool createTable();?? ? ?? ?static bool ThreadInternetConnect(PVOID )? ?? ?//打開目標ftp ?? ?static bool createFolder(CString temp1, CString temp2, CString temp3); ?//上傳文件 ?? ?static bool insert(CString temp, CString temp1); ? ? ? ? ? ? ? ? ? ? ? ?//出入數(shù)據 ?? ?static bool createClose(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//斷開連接 };
//ftpclass.cpp #include "stdafx.h" #include "ftpclass.h" CString FtpClass::ftp_Ip = TEXT("192.168.3.104"); CString FtpClass::ftp_Port = TEXT("21"); CString FtpClass::ftp_User = TEXT("Administrator"); CString FtpClass::ftp_Password = TEXT("xinxin"); CString FtpClass::ftp_Fixed_Path = TEXT("1級固定目錄/2級固定目錄/3級固定目錄"); CString FtpClass::ftp_Free_Path = TEXT("自動生成目錄"); BOOL ?FtpClass::pRes = false; HINTERNET FtpClass::hInternet = NULL; HINTERNET FtpClass::hConnect = NULL; //創(chuàng)建一個連接 bool FtpClass::createConnection() { ?? ?/*ftp_Ip = TEXT("Ini讀取"); ?? ?ftp_Port = TEXT("Ini讀取"); ?? ?ftp_User = TEXT("Ini讀取"); ?? ?ftp_Password = TEXT("Ini讀取"); ?? ?ftp_Fixed_Path = TEXT("Ini讀取");*/ ?? ?hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, ?? ??? ?NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE); ?? ?NULL_VERIFY(hInternet,1); } bool FtpClass::ThreadInternetConnect(PVOID param) { ?// 打開http ? ? ? ?hConnect = InternetConnect(hInternet, ftp_Ip, INTERNET_DEFAULT_FTP_PORT,//INTERNET_DEFAULT_FTP_PORT ?第三個參數(shù)默認值21 ? ftp_User, ftp_Password, INTERNET_SERVICE_FTP, ? INTERNET_FLAG_EXISTING_CONNECT || INTERNET_FLAG_PASSIVE, 0); ?return 1; } //打開目標ftp bool FtpClass::createTable() { ?/*hConnect = InternetConnect(hInternet, ftp_Ip, 25,//INTERNET_DEFAULT_FTP_PORT ?第三個參數(shù)默認值21 ? ftp_User, ftp_Password, INTERNET_SERVICE_FTP, ? INTERNET_FLAG_EXISTING_CONNECT || INTERNET_FLAG_PASSIVE, 0); ?NULL_VERIFY(hConnect,2);*/ ?HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadInternetConnect, (LPVOID)NULL, 0, NULL);? ?//超時3秒,如果等待結果是超時 ?if (WaitForSingleObject(hThread, 3 * 1000) == WAIT_TIMEOUT) { ? TerminateThread(hThread, 0); ? CloseHandle(hThread); ? NULL_VERIFY(hConnect, 2); ? ?} ?NULL_VERIFY(hConnect, 2); } //上傳文件 bool FtpClass::createFolder(CString temp1, CString temp2, CString temp3) { ?? ?/*新建文件件每次只能創(chuàng)建一級,多個需要分多次創(chuàng)建*/ ?? ?pRes = false;?? ? ?? ?ftp_Free_Path = ""; ?? ?ftp_Free_Path = ftp_Fixed_Path ?+ "/" + temp1; ?? ?FtpCreateDirectory(hConnect, ftp_Free_Path); ?? ?ftp_Free_Path = ftp_Free_Path + "/" + temp2; ?? ?FtpCreateDirectory(hConnect, ftp_Free_Path); ?? ?ftp_Free_Path = ftp_Free_Path + "/" + temp3; ?? ?pRes = FtpCreateDirectory(hConnect, ftp_Free_Path); ?? ?BOOL_VERIFY(pRes,3); } //出入數(shù)據 bool FtpClass::insert(CString temp, CString temp1) { ?? ?pRes = false; ?? ?ftp_Free_Path = ftp_Free_Path + "/" +temp1; ?? ?pRes = FtpPutFile(hConnect, temp,/*本機文件*/ ?? ??? ?ftp_Free_Path, ?/*TEXT("一級目錄/二級目錄/三級目錄/a.txt"),*/ ?? ??? ?FTP_TRANSFER_TYPE_ASCII, 0); ?? ?BOOL_VERIFY(pRes,4); } //斷開連接 bool FtpClass::createClose() {?? ? ?? ?pRes = false; ?? ?if (InternetCloseHandle(hConnect)) ?? ??? ?pRes = InternetCloseHandle(hInternet); ?? ?BOOL_VERIFY(pRes,5); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++ for循環(huán)與nullptr的小知識點分享
這篇文章主要是來和大家介紹一些C++中的小知識點,本文分享的是for循環(huán)與nullptr,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-05-05C++11/14 線程的創(chuàng)建與分離的實現(xiàn)
這篇文章主要介紹了C++11/14 線程的創(chuàng)建與分離的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01