谁来帮我看看这段代码
我在运行的时候经常会“文件传输失败”(提示信息自己写的)
即使提示“文件传输成功”也无法再本地的文件夹看到文件
参照的资料是这个
http://www.360doc.com/content/09/1027/00/332670_7915698.shtml
找不到解决方法了,请各位比较厉害的前来帮忙一下// FTPdownload.cpp : Defines the entry point for the console application.
//#include "stdafx.h"#include <afxinet.h>bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */);#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0; // initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}
bool state;
//bool TURE=1;
const char pcLocalFile[100]="/****";//本地文件名
const char pcRemoteFile[100]="/****";//需要从服务器上下载的东西
const char pcServer[50]="**.**.**.**";//ip地址
const char pcUserName[6]="***";//用户名
const char pcPassword[10]="***";//密码// printf("请输入下到本机的路径:\n");// scanf("%s",pcLocalFile); printf("正在下载,请稍后1\n");
state=FtpDownload(pcLocalFile,pcRemoteFile,pcServer,pcUserName,pcPassword,21,TRUE);
printf("正在下载,请稍后2\n");
if(state)
{
printf("下载完毕!\n");
}
else
{
printf("下载失败,请从新运行本程序!\n");
}
Sleep(20000);
return nRetCode;
}
bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */)
{
CInternetSession * pInetSession = NULL;
CFtpConnection * pFtpConnection = NULL;
bool bRet = false;
if (pInetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS))
{
pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
pInetSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000); TRACE("[FTP Addr]\t\t%s\n", pcServer);
TRACE("[FTP User Name]\t%s\n", pcUserName);
TRACE("[FTP Password]\t%s\n", pcPassword);
try
{
pFtpConnection = pInetSession->GetFtpConnection(pcServer, pcUserName, pcPassword, nPort, bPassive);
}
catch (CInternetException *pEx)
{
pFtpConnection = NULL;
pEx->ReportError(MB_ICONEXCLAMATION);
pEx->Delete();
}
if (pFtpConnection)
{
TRACE("[Download Src]\t\t%s\n", pcRemoteFile);
TRACE("[Download Dst]\t\t%s\n", pcLocalFile);
bRet = pFtpConnection->GetFile(pcRemoteFile,pcLocalFile) ? true : false;//这是用来实现文件的传输的,成功则返回true,失败则返回false
if(bRet)
{
printf("传输文件成功\n");
}else
{
printf("传输文件失败\n");
}
}
}
if (pInetSession)
{
pInetSession->Close();
delete pInetSession;
}
if (pFtpConnection)
{
pFtpConnection->Close();
delete pFtpConnection;
}
return bRet;
}
我在运行的时候经常会“文件传输失败”(提示信息自己写的)
即使提示“文件传输成功”也无法再本地的文件夹看到文件
参照的资料是这个
http://www.360doc.com/content/09/1027/00/332670_7915698.shtml
找不到解决方法了,请各位比较厉害的前来帮忙一下// FTPdownload.cpp : Defines the entry point for the console application.
//#include "stdafx.h"#include <afxinet.h>bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */);#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0; // initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}
bool state;
//bool TURE=1;
const char pcLocalFile[100]="/****";//本地文件名
const char pcRemoteFile[100]="/****";//需要从服务器上下载的东西
const char pcServer[50]="**.**.**.**";//ip地址
const char pcUserName[6]="***";//用户名
const char pcPassword[10]="***";//密码// printf("请输入下到本机的路径:\n");// scanf("%s",pcLocalFile); printf("正在下载,请稍后1\n");
state=FtpDownload(pcLocalFile,pcRemoteFile,pcServer,pcUserName,pcPassword,21,TRUE);
printf("正在下载,请稍后2\n");
if(state)
{
printf("下载完毕!\n");
}
else
{
printf("下载失败,请从新运行本程序!\n");
}
Sleep(20000);
return nRetCode;
}
bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */)
{
CInternetSession * pInetSession = NULL;
CFtpConnection * pFtpConnection = NULL;
bool bRet = false;
if (pInetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS))
{
pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
pInetSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000); TRACE("[FTP Addr]\t\t%s\n", pcServer);
TRACE("[FTP User Name]\t%s\n", pcUserName);
TRACE("[FTP Password]\t%s\n", pcPassword);
try
{
pFtpConnection = pInetSession->GetFtpConnection(pcServer, pcUserName, pcPassword, nPort, bPassive);
}
catch (CInternetException *pEx)
{
pFtpConnection = NULL;
pEx->ReportError(MB_ICONEXCLAMATION);
pEx->Delete();
}
if (pFtpConnection)
{
TRACE("[Download Src]\t\t%s\n", pcRemoteFile);
TRACE("[Download Dst]\t\t%s\n", pcLocalFile);
bRet = pFtpConnection->GetFile(pcRemoteFile,pcLocalFile) ? true : false;//这是用来实现文件的传输的,成功则返回true,失败则返回false
if(bRet)
{
printf("传输文件成功\n");
}else
{
printf("传输文件失败\n");
}
}
}
if (pInetSession)
{
pInetSession->Close();
delete pInetSession;
}
if (pFtpConnection)
{
pFtpConnection->Close();
delete pFtpConnection;
}
return bRet;
}
---- BOOL GetFile(LPCTSTR pstrRemoteFile,LPCTSTR pstrLocalFile,BOOL bFailExists ,DWORD dwAttributes,DWORD dwFlags,DWORD dwContext);
---- 调用这个成员函数,可以从FTP服务器取得文件,并且把文件保存在本地机器上。GetFile()函数是一个比较高级的例程,它可以处理所有有关从FTP服务器读文件,以及把文件存放在本地机器上的工作。如果dwFlags为FILE_TRANSFER_TYPE_ASCII,文件数据的传输也会把控制和格式符转化为Windows中的等阶符号。默认的传输模式是二进制模式,文件会以和服务器上相同的格式被下载。
---- pstrRemoteFile和 pstrLocalFile可以是相对于当前目录的部分文件名,也可以是全文件名,在这两个名字中间,都既可以用反斜杠(\)或者正斜杠(/)来作为文件名的目录分隔符,GetFile()在使用前会把目录分隔符转化为适当的字符。
---- 可以用自己选择的值来取代dwContext默认的值,设置为上下文标识符与CFtpConnection对象的定位操作有关,这个操作由CFtpConnection中的CInternetSession对象创建。返回给CInternetSession::OnStatusCallBack的值指出了所标识操作的状态。
---- 如果调用成功,函数的返回为非0,否则返回0,如果调用失败,可以调用Win32函数GetLastError(),确认出错的原因。
pcRemoteFile我设置的是服务器上的exe
pcLocalFile我设置的是当前文件夹下的test文件夹
who can help me?
// //#include "stdafx.h" #include <afx.h>
#include <afxdb.h>
#include <afxinet.h>
#include <iostream>
using namespace std;#define APP_NAME_STR "FTP_DOWNLOAD"bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */); #ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif /////////////////////////////////////////////////////////////////////////////
// The one and only application object //CWinApp theApp;
//
using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
} bool state; ////////////////////////////////////////////////////////////////////
//
// 这里是我本地测试用到的FTP服务器信息,你在测试时换成你的FTP服务器信息
//
////////////////////////////////////////////////////////////////////
const char pcLocalFile[100]="LocalTest.c";//本地文件名
const char pcRemoteFile[100]="test.c";//需要从服务器上下载的东西
const char pcServer[50]="192.168.3.42";//ip地址
const char pcUserName[6]="1";//用户名
const char pcPassword[10]="1";//密码
// printf("请输入下到本机的路径:\n");
// scanf("%s",pcLocalFile);
printf("正在下载,请稍后1\n");
state=FtpDownload(pcLocalFile,pcRemoteFile,pcServer,pcUserName,pcPassword,21,TRUE);
printf("正在下载,请稍后2\n");
if(state)
{
printf("下载完毕!\n");
}
else
{
printf("下载失败,请从新运行本程序!\n");
}
Sleep(20000);
return nRetCode;
}
bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */)
{
CInternetSession * pInetSession = NULL;
CFtpConnection * pFtpConnection = NULL;
bool bRet = false;
if (pInetSession = new CInternetSession(APP_NAME_STR, 1, PRE_CONFIG_INTERNET_ACCESS))
{
pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
pInetSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000);
TRACE("[FTP Addr]\t\t%s\n", pcServer);
TRACE("[FTP User Name]\t%s\n", pcUserName);
TRACE("[FTP Password]\t%s\n", pcPassword);
try
{
pFtpConnection = pInetSession->GetFtpConnection(pcServer, pcUserName, pcPassword, nPort, bPassive);
}
catch (CInternetException *pEx)
{
pFtpConnection = NULL;
pEx->ReportError(MB_ICONEXCLAMATION);
pEx->Delete();
}
if (pFtpConnection)
{
TRACE("[Download Src]\t\t%s\n", pcRemoteFile);
TRACE("[Download Dst]\t\t%s\n", pcLocalFile);
bRet = pFtpConnection->GetFile(pcRemoteFile,pcLocalFile) ? true : false;//这是用来实现文件的传输的,成功则返回true,失败则返回false
if(bRet)
{
printf("传输文件成功\n");
}else
{
printf("传输文件失败\n");
}
}
}
if (pInetSession)
{
pInetSession->Close();
delete pInetSession;
}
if (pFtpConnection)
{
pFtpConnection->Close();
delete pFtpConnection;
}
return bRet;
}
输出:正在下载,请稍后1
传输文件成功
正在下载,请稍后2
下载完毕!