博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
充满梦想的FTP探索之旅(二)WinInet和FTP
阅读量:4120 次
发布时间:2019-05-25

本文共 3207 字,大约阅读时间需要 10 分钟。

需要

object content
Header Wininet.h
Library Wininet.lib
DLL Wininet.dll

变量声明

BOOL            bSuccess ;HINTERNET       hIntSession, hFtpSession, hFind ;HANDLE hLocalFind ;WIN32_FIND_DATA finddata ;

Open an internet session

hIntSession = InternetOpen (CTelProxyApp::szTitle,           INTERNET_OPEN_TYPE_DIRECT,            NULL, NULL, 0) ;if(hIntSession == NULL){    DWORD dwError = GetLastError();    ErrLog(<<"InternetOpen error "<

Open an FTP session

hFtpSession = InternetConnect(hIntSession, m_szFTPServer,                INTERNET_DEFAULT_FTP_PORT,                m_szUser, m_szPassword, INTERNET_SERVICE_FTP, 0, 0); if(hFtpSession == NULL) {     DWORD dwError = GetLastError();     ErrLog(<<"InternetConnect error "<

ftp远程路径设置

//根目录为"/"bSuccess = FtpSetCurrentDirectory (hFtpSession, m_szDirectory) ;if(!bSuccess){    DWORD dwLastError = GetLastError();         if (dwLastError == ERROR_INTERNET_EXTENDED_ERROR) {        char szLastResponse[1024] = { 0 };         DWORD lastErrorResponse = 0;         DWORD bufSize = sizeof(szLastResponse);         if (InternetGetLastResponseInfo(&lastErrorResponse, szLastResponse, &bufSize))         {                           //如果错误类似于550 /0: No such file or directory.            if(memcmp(szLastResponse,"550",3) == 0)            {                bSuccess = FtpCreateDirectory (hFtpSession, m_szDirectory) ;                bSuccess = FtpSetCurrentDirectory (hFtpSession, m_szDirectory) ;                if(!bSuccess)                {                    ErrLog(<
<<"Cannot set directory to "<

}

获得ftp远程路径

char szCurrentDirectory[MAX_PATH] = {0};DWORD dwLen = MAX_PATH;bSuccess = FtpGetCurrentDirectory(hFtpSession, szCurrentDirectory, &dwLen);if(!bSuccess){    DWORD dwLastError = GetLastError(); //set dwLastError to GetLastError()    ErrLog(<<"Cannot set directory to "<

}

本地路径遍历上传

hLocalFind = ::FindFirstFile(CApp::getRecordFileTemplate().c_str(),&finddata);if(hLocalFind == INVALID_HANDLE_VALUE){    InternetCloseHandle (hFtpSession) ;    InternetCloseHandle (hIntSession) ;         return;}do{    bSuccess = FtpPutFile(hFtpSession,(CApp::getRecordFileDirectory()+=finddata.cFileName).c_str(),                    finddata.cFileName,FTP_TRANSFER_TYPE_BINARY, 0); if(!bSuccess){    DWORD dwErr = GetLastError();    ErrLog(<<"upload file to ftp server fail.error "<

远程路径遍历下载

hFind = FtpFindFirstFile (hFtpSession, TEMPLATE,                            &finddata, 0, 0) ; if (hFind == NULL) {      InternetCloseHandle (hFtpSession) ;      InternetCloseHandle (hIntSession) ;      return; } do  {                    FtpGetFile (hFtpSession,                   finddata.cFileName, finddata.cFileName, TRUE,                   FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0) ; } while (InternetFindNextFile (hFind, &finddata)) ;

完成销毁对象

InternetCloseHandle (hFind) ;InternetCloseHandle (hFtpSession) ;InternetCloseHandle (hIntSession) ;

注意

You should call theGetLastError function immediately when a function’s return value indicates that such a call will return useful data.

源文档

否则遇到错误,调用GetLastError查看错误码返回0.我之前没注意这个问题,日志输出放在了GetlastError前面,结果ftp函数返回FALSE,查看GetLastError的错误码为0,颇是束手无策。

参考

• <
> WinInet和FTP• <
> FTP协议的实现• WinINet Functions

转载地址:http://ewppi.baihongyu.com/

你可能感兴趣的文章
Day_15JavaSE 异常
查看>>
异常 Java学习Day_15
查看>>
JavaSE_day_03 方法
查看>>
day-03JavaSE_循环
查看>>
Mysql初始化的命令
查看>>
day_21_0817_Mysql
查看>>
day-22 mysql_SQL 结构化查询语言
查看>>
MySQL关键字的些许问题
查看>>
浅谈HTML
查看>>
css基础
查看>>
HTML&CSS进阶
查看>>
Servlet进阶和JSP基础
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
MySQL-分布式架构-MyCAT
查看>>
设计模式六大原则(6):开闭原则
查看>>
阿里面试总结--JAVA
查看>>
Servlet的生命周期
查看>>