QT實(shí)現(xiàn)FTP上傳文件
本文實(shí)例為大家分享了QT實(shí)現(xiàn)FTP上傳文件的具體代碼,供大家參考,具體內(nèi)容如下
兩臺(tái)電腦通過(guò)網(wǎng)線建立本地連接,保證網(wǎng)關(guān)在同一段;

服務(wù)器端打開(kāi)ftp;
控制面板→程序→啟用或關(guān)閉windows功能→windows功能→Internet信息服務(wù)
啟用“FTP服務(wù)”FTP擴(kuò)展性” IIS管理控制臺(tái)”

開(kāi)始屏幕的搜索中輸入“IIS”,然后點(diǎn)擊打開(kāi)“IIS管理器”
打開(kāi)“IIS管理器”后,在左欄的“網(wǎng)站”上點(diǎn)擊右鍵,打開(kāi)“添加FTP站點(diǎn)”
然后按照提示填寫(xiě)站點(diǎn)信息
點(diǎn)擊“下一步”,按照下圖提示,設(shè)置“綁定和SSL設(shè)置”,在“IP地址”處,可以用內(nèi)網(wǎng)IP也可以用外網(wǎng)IP,訪客自然也就根據(jù)你的IP設(shè)定來(lái)決定;
點(diǎn)擊“下一步”,設(shè)置“身份驗(yàn)證和授權(quán)信息”
然后在本機(jī)瀏覽器地址欄中輸入“ftp://填寫(xiě)的IP”測(cè)試一下
客戶端網(wǎng)頁(yè)測(cè)試遠(yuǎn)程訪問(wèn);
客戶端(另一臺(tái)電腦)瀏覽器地址欄中輸入“ftp://填寫(xiě)的IP”測(cè)試一下
客戶端cmd測(cè)試遠(yuǎn)程訪問(wèn);
win+r打開(kāi)運(yùn)行窗口,輸入cmd
回車(chē)打開(kāi)cmd命令窗口
cmd命令中輸入:ftp回車(chē)
回車(chē)切換至ftp命令窗口,輸入命令:open,回車(chē)提示:到
到即所要連接測(cè)試的ftp地址,我們輸入:IP 22
即:ip地址+空格+端口號(hào),沒(méi)有+號(hào)
回車(chē)后彈出用戶一行,輸入ftp的用戶名后回車(chē),輸入ftp用戶名對(duì)應(yīng)的密碼
輸入密碼后回車(chē),如果提示,user logged in就說(shuō)么ftp創(chuàng)建無(wú)問(wèn)題
客戶端程序測(cè)試遠(yuǎn)程訪問(wèn)。
新建程序,添加ftpclass.cpp、ftpclass.h,復(fù)制main.cpp內(nèi)容到程序入口函數(shù)
注意:/項(xiàng)目-屬性-常規(guī)-字符集-使用多字節(jié)字符集/

//main.cpp
#include "stdafx.h"
#include "ftpclass.h"
void main()
{
?? ?printf("------- 開(kāi)始測(cè)試!------\n");
?? ?printf("01--創(chuàng)建連接 %d\n", FtpClass::createConnection());
?? ?printf("02--打開(kāi)目標(biāo)ftp %d\n", FtpClass::createTable());?? ?
?? ?
?? ?/*可以讀取ini內(nèi)參數(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("自動(dòng)生成目錄1","自動(dòng)生成目錄2","自動(dòng)生成目錄3"));?? ?
?? ?/*上傳目標(biāo)路徑*/
?? ?printf("04--上傳文件 %d\n", FtpClass::insert( "D:/a.txt", "b.txt"));
?? ?/*本機(jī)文件需要上傳文件*/ ?/*上傳后文件名稱,可以和本地文件名稱不一樣,類型最好別換*/
?? ?
?? ?printf("05--關(guān)閉通訊 %d\n", FtpClass::createClose());
?? ?printf("------ 結(jié)束測(cè)試!------\n");
?? ?
?? ?return ;
}//ftpclass.h
/*項(xiàng)目-屬性-常規(guī)-字符集-使用多字節(jié)字符集*/
/*wininet.lib、shlwapi.lib可以直接添加到附加依賴項(xiàng)*/
/*BOOL_VERIFY、NULL_VERIFY 程序結(jié)束判斷*/
#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:關(guān)閉窗口句柄失敗!\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:打開(kāi)通訊錯(cuò)誤 Error:%d\n", GetLastError());}\
if (switch_null == 2) {printf(" ? ? ?FTP_02_err:建立連接錯(cuò)誤 Error:%d\n", GetLastError());}\
return false;\
}
#include "stdafx.h"http://沒(méi)用
#include <afxinet.h>//MFC相關(guān)
#include "wininet.h"http://調(diào)用FTP相關(guān)類
#include "shlwapi.h"http://調(diào)用文件操作相關(guān)類
class FtpClass
{
public:?? ?
?? ?/*ini讀取變量*/
?? ?static CString ftp_Ip;//目標(biāo)ip
?? ?static CString ftp_Port;//目標(biāo)端口
?? ?static CString ftp_User;//目標(biāo)賬戶
?? ?static CString ftp_Password;//目標(biāo)密碼
?? ?static CString ftp_Fixed_Path;//目標(biāo)固定路徑
?? ?static CString ftp_Free_Path;//目標(biāo)自己生成路徑
?? ?
?? ? /*全局變量*/
?? ?static BOOL ?pRes;
?? ?static HINTERNET hInternet;
?? ?static HINTERNET hConnect;
?? ?
?? ?/*全局函數(shù)*/
?? ?static bool createConnection();?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?//創(chuàng)建一個(gè)連接
?? ?static bool createTable();?? ?
?? ?static bool ThreadInternetConnect(PVOID )?
?? ?//打開(kāi)目標(biāo)ftp
?? ?static bool createFolder(CString temp1, CString temp2, CString temp3); ?//上傳文件
?? ?static bool insert(CString temp, CString temp1); ? ? ? ? ? ? ? ? ? ? ? ?//出入數(shù)據(jù)
?? ?static bool createClose(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//斷開(kāi)連接
};//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級(jí)固定目錄/2級(jí)固定目錄/3級(jí)固定目錄");
CString FtpClass::ftp_Free_Path = TEXT("自動(dòng)生成目錄");
BOOL ?FtpClass::pRes = false;
HINTERNET FtpClass::hInternet = NULL;
HINTERNET FtpClass::hConnect = NULL;
//創(chuàng)建一個(gè)連接
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) {
?// 打開(kāi)http ? ? ?
?hConnect = InternetConnect(hInternet, ftp_Ip, INTERNET_DEFAULT_FTP_PORT,//INTERNET_DEFAULT_FTP_PORT ?第三個(gè)參數(shù)默認(rèn)值21
? ftp_User, ftp_Password, INTERNET_SERVICE_FTP,
? INTERNET_FLAG_EXISTING_CONNECT || INTERNET_FLAG_PASSIVE, 0);
?return 1;
}
//打開(kāi)目標(biāo)ftp
bool FtpClass::createTable()
{
?/*hConnect = InternetConnect(hInternet, ftp_Ip, 25,//INTERNET_DEFAULT_FTP_PORT ?第三個(gè)參數(shù)默認(rèn)值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);?
?//超時(shí)3秒,如果等待結(jié)果是超時(shí)
?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)建一級(jí),多個(gè)需要分多次創(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ù)據(jù)
bool FtpClass::insert(CString temp, CString temp1)
{
?? ?pRes = false;
?? ?ftp_Free_Path = ftp_Free_Path + "/" +temp1;
?? ?pRes = FtpPutFile(hConnect, temp,/*本機(jī)文件*/
?? ??? ?ftp_Free_Path, ?/*TEXT("一級(jí)目錄/二級(jí)目錄/三級(jí)目錄/a.txt"),*/
?? ??? ?FTP_TRANSFER_TYPE_ASCII, 0);
?? ?BOOL_VERIFY(pRes,4);
}
//斷開(kāi)連接
bool FtpClass::createClose()
{?? ?
?? ?pRes = false;
?? ?if (InternetCloseHandle(hConnect))
?? ??? ?pRes = InternetCloseHandle(hInternet);
?? ?BOOL_VERIFY(pRes,5);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VScode編譯C++ 頭文件顯示not found的問(wèn)題
這篇文章主要介紹了VScode編譯C++ 頭文件顯示not found的問(wèn)題,本文給大家分享問(wèn)題解決方法,通過(guò)截圖的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-03-03
Opencv實(shí)現(xiàn)視頻播放與進(jìn)度控制
這篇文章主要為大家詳細(xì)介紹了Opencv實(shí)現(xiàn)視頻播放與進(jìn)度控制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C++ 編寫(xiě)DLL文件給易語(yǔ)言調(diào)用方法
在本文中我們給大家分享了C++ 編寫(xiě)DLL文件給易語(yǔ)言調(diào)用的代碼和方法,需要的朋友們學(xué)習(xí)下。2019-01-01
C++常見(jiàn)獲取隨機(jī)數(shù)的方法小結(jié)
這篇文章主要介紹了C++常見(jiàn)獲取隨機(jī)數(shù)的方法,結(jié)合實(shí)例形式總結(jié)分析了C++獲取隨機(jī)數(shù)的幾種常見(jiàn)方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-05-05
c/c++拷貝構(gòu)造函數(shù)和關(guān)鍵字explicit詳解
這篇文章主要介紹了c/c++拷貝構(gòu)造函數(shù)和關(guān)鍵字explicit的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array實(shí)例詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
詳解在C++中顯式默認(rèn)設(shè)置的函數(shù)和已刪除的函數(shù)的方法
這篇文章主要介紹了在C++中顯式默認(rèn)設(shè)置的函數(shù)和已刪除的函數(shù)的方法,文中講到了C++11標(biāo)準(zhǔn)中的新特性,需要的朋友可以參考下2016-01-01

