上传文件到 FTP 服务器和从 FTP 服务器下载文件的基础知识






4.12/5 (18投票s)
2002年6月29日
2分钟阅读

165405
本教程帮助您上传和下载 FTP 服务器上的文件。
引言
本文解释并演示了使用文件传输协议 (FTP) 将您的文件上传到互联网上的服务器以及从 FTP 服务器下载文件的用法。 这需要您拥有要上传文件的 FTP 服务器的用户名和密码,或者要从中下载文件的 FTP 服务器的用户名和密码。
用法
FTP 有许多用途,其中我可以给您一些初学者示例。 您可以使用它将数据存储在服务器上,您的同事可以在不同的位置访问这些数据。 我使用它的方式是为了在一个地方显示 PowerPoint 幻灯片,并同时将其广播到许多客户端。 该软件捕获屏幕截图并将其保存为 .jpg 文件在 ftp 服务器上。 我的客户端软件会不断访问该服务器,检查是否有新文件,并在进行更改时显示它们。 客户端软件基本上是一个 JPEG 查看器软件。 您可以根据您的需求设计更多的用法。
实现
要使用 ftp,您需要添加 afxinet.h 头文件。
- 在您的对话框的头文件中添加以下语句,或者如果是一个文档/视图项目,则将其添加到您的文档类头文件中。
#include "afxinet.h"
CFtpConnection *m_pFtpConnection; CInternetSession m_Session;
OnInitDialog
或 InitInstance
函数)中,添加以下行。m_pFtpConnection = NULL; try { // Here usr is the username, pwd is the password // and ftpsite.com is the name of the ftp site which // you want to connect to. m_pFtpConnection = m_Session.GetFtpConnection("ftpSite.com", "usr","pwd",INTERNET_INVALID_PORT_NUMBER); } catch(CInternetException *pEx) { pEx->ReportError(MB_ICONEXCLAMATION); m_pFtpConnection = NULL; pEx->Delete(); } return TRUE;
CFileFind Finder; CString strFileName; // Here c:\\Myfile.bmp is the name of the file that you want // to upload. It neednt necessarily be a bitmap file. You // can upload any file that you want to. // The CString strFileName is used so that the same name // is uploaded to the ftp server. // After uploading, the file in the ftp server will have // the same name as your local file. // You can also rename it to anything if(Finder.FindFile("C:\\Myfile.bmp")==TRUE) { Finder.FindNextFile(); strFileName = Finder.GetFileName(); Finder.Close(); } BOOL bUploaded = m_pFtpConnection->PutFile("C:\\Myfile.bmp", strFileName,FTP_TRANSFER_TYPE_BINARY,1); AfxMessageBox("Uploaded Successfully");
m_pFtpConnection->GetFile("File.ext","C:\\File.ext", TRUE,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY,1);
m_Session.Close(); m_pFtpConnection->Close(); if(m_pFtpConnection!=NULL) delete m_pFtpConnection;
修订历史
- 2002 年 7 月 31 日 - 初始修订版 [基本修订版]
结论
FTP 易于使用且用途广泛。 思考您自己的实现和可以添加到提供的代码中的各种功能。 就这样。祝您好运,度过愉快的时光。