網站首頁 編程語言 正文
本文實例為大家分享了QT實現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創建無問題
客戶端程序測試遠程訪問。
新建程序,添加ftpclass.cpp、ftpclass.h,復制main.cpp內容到程序入口函數
注意:/項目-屬性-常規-字符集-使用多字節字符集/
//main.cpp
#include "stdafx.h"
#include "ftpclass.h"
void main()
{
?? ?printf("------- 開始測試!------\n");
?? ?printf("01--創建連接 %d\n", FtpClass::createConnection());
?? ?printf("02--打開目標ftp %d\n", FtpClass::createTable());?? ?
?? ?
?? ?/*可以讀取ini內參數
?? ?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--創建文件夾 %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
/*項目-屬性-常規-字符集-使用多字節字符集*/
/*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:創建文件夾失??!%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;
?? ?
?? ?/*全局函數*/
?? ?static bool createConnection();?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?//創建一個連接
?? ?static bool createTable();?? ?
?? ?static bool ThreadInternetConnect(PVOID )?
?? ?//打開目標ftp
?? ?static bool createFolder(CString temp1, CString temp2, CString temp3); ?//上傳文件
?? ?static bool insert(CString temp, CString temp1); ? ? ? ? ? ? ? ? ? ? ? ?//出入數據
?? ?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;
//創建一個連接
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 ?第三個參數默認值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 ?第三個參數默認值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)
{
?? ?/*新建文件件每次只能創建一級,多個需要分多次創建*/
?? ?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);
}
//出入數據
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
相關推薦
- 2023-03-29 Android?Flutter中Offstage組件的使用教程詳解_Android
- 2022-12-25 Android?Flutter實現評分組件的示例代碼_Android
- 2022-05-02 Python中的變量和數據類型詳情_python
- 2022-02-03 ionic集成極光推送之點擊推送跳轉到指定頁面
- 2022-07-07 go語言中如何使用select的實現示例_Golang
- 2022-05-13 iOS msgSend消息發送流程
- 2022-10-15 Windows10搭建FTP服務器詳細教程_FTP服務器
- 2022-08-13 VMware vCenter 無法創建自定義規范
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支