日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

QT實現(xiàn)FTP上傳文件_C 語言

作者:Beyond欣 ? 更新時間: 2022-10-15 編程語言

本文實例為大家分享了QT實現(xiàn)FTP上傳文件的具體代碼,供大家參考,具體內(nèi)容如下

兩臺電腦通過網(wǎng)線建立本地連接,保證網(wǎng)關(guān)在同一段;

服務(wù)器端打開ftp;

控制面板→程序→啟用或關(guān)閉windows功能→windows功能→Internet信息服務(wù)
啟用“FTP服務(wù)”FTP擴展性” IIS管理控制臺”

開始屏幕的搜索中輸入“IIS”,然后點擊打開“IIS管理器”
打開“IIS管理器”后,在左欄的“網(wǎng)站”上點擊右鍵,打開“添加FTP站點”
然后按照提示填寫站點信息
點擊“下一步”,按照下圖提示,設(shè)置“綁定和SSL設(shè)置”,在“IP地址”處,可以用內(nèi)網(wǎng)IP也可以用外網(wǎng)IP,訪客自然也就根據(jù)你的IP設(shè)定來決定;
點擊“下一步”,設(shè)置“身份驗證和授權(quán)信息”
然后在本機瀏覽器地址欄中輸入“ftp://填寫的IP”測試一下

客戶端網(wǎng)頁測試遠程訪問;

客戶端(另一臺電腦)瀏覽器地址欄中輸入“ftp://填寫的IP”測試一下

客戶端cmd測試遠程訪問;

win+r打開運行窗口,輸入cmd
回車打開cmd命令窗口
cmd命令中輸入:ftp回車
回車切換至ftp命令窗口,輸入命令:open,回車提示:到
到即所要連接測試的ftp地址,我們輸入:IP 22
即:ip地址+空格+端口號,沒有+號
回車后彈出用戶一行,輸入ftp的用戶名后回車,輸入ftp用戶名對應(yīng)的密碼
輸入密碼后回車,如果提示,user logged in就說么ftp創(chuàng)建無問題

客戶端程序測試遠程訪問。

新建程序,添加ftpclass.cpp、ftpclass.h,復(fù)制main.cpp內(nèi)容到程序入口函數(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內(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("自動生成目錄1","自動生成目錄2","自動生成目錄3"));?? ?
?? ?/*上傳目標路徑*/

?? ?printf("04--上傳文件 %d\n", FtpClass::insert( "D:/a.txt", "b.txt"));
?? ?/*本機文件需要上傳文件*/ ?/*上傳后文件名稱,可以和本地文件名稱不一樣,類型最好別換*/
?? ?
?? ?printf("05--關(guān)閉通訊 %d\n", FtpClass::createClose());
?? ?printf("------ 結(jié)束測試!------\n");
?? ?
?? ?return ;
}
//ftpclass.h
/*項目-屬性-常規(guī)-字符集-使用多字節(jié)字符集*/
/*wininet.lib、shlwapi.lib可以直接添加到附加依賴項*/
/*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:打開通訊錯誤 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相關(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;//目標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ù)據(jù)
?? ?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秒,如果等待結(jié)果是超時
?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ù)據(jù)
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);
}

原文鏈接:https://blog.csdn.net/a15005784320/article/details/98611713

欄目分類
最近更新