C++实现向FTP上传文件

文章来源: http://www.itnose.net/detail/6034891.html
更多文章: http://www.itnose.net/type/55.html
连接

 

    CInternetSession *m_pInetsession;  
    CFtpConnection *m_pFtpConnection;  
    m_pInetsession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);  
    try  
    {  
         m_pFtpConnection=m_pInetsession->GetFtpConnection("127.0.0.1",NULL,NULL,38);  
         MessageBox("连接成功");  
    }  
    catch(CInternetException *pEx)  
    {  
        TCHAR szError[1024];  
        if(pEx->GetErrorMessage(szError,1024))  
            AfxMessageBox(szError);  
        else  
            AfxMessageBox("There was an exception");  
        pEx->Delete();  
        m_pFtpConnection=NULL;  
        return;  
    }  

 


下载文件

BOOL GetFile( LPCTSTRpstrRemoteFile, LPCTSTRpstrLocalFile, BOOLbFailIfExists = TRUE, DWORDdwAttributes = FILE_ATTRIBUTE_NORMAL, DWORDdwFlags = FTP_TRANSFER_TYPE_BINARY, DWORDdwContext = 1 );

pstrRemoteFile

A pointer to a null-terminated string containing the name of a file to retrieve from the FTP server.

pstrLocalFile

A pointer to a null-terminated string containing the name of the file to create on the local system.

bFailIfExists

Indicates whether the file name may already be used by an existing file. If the local file name already exists, and this parameter isTRUE,GetFile fails. Otherwise, GetFile will erase the existing copy of the file.

dwAttributes

Indicates the attributes of the file. This can be any combination of the following FILE_ATTRIBUTE_* flags.

  • FILE_ATTRIBUTE_ARCHIVE   The file is an archive file. Applications use this attribute to mark files for backup or removal.

  • FILE_ATTRIBUTE_COMPRESSED   The file or directory is compressed. For a file, compression means that all of the data in the file is compressed. For a directory, compression is the default for newly created files and subdirectories.

  • FILE_ATTRIBUTE_DIRECTORY   The file is a directory.

  • FILE_ATTRIBUTE_NORMAL   The file has no other attributes set. This attribute is valid only if used alone. All other file attributes override FILE_ATTRIBUTE_NORMAL:

  • FILE_ATTRIBUTE_HIDDEN   The file is hidden. It is not to be included in an ordinary directory listing.

  • FILE_ATTRIBUTE_READONLY   The file is read only. Applications can read the file but cannot write to it or delete it.

  • FILE_ATTRIBUTE_SYSTEM   The file is part of or is used exclusively by the operating system.

  • FILE_ATTRIBUTE_TEMPORARY   The file is being used for temporary storage. Applications should write to the file only if absolutely necessary. Most of the file’s data remains in memory without being flushed to the media because the file will soon be deleted.

dwFlags

Specifies the conditions under which the transfer occurs. This parameter can be any of thedwFlags values described inFtpGetFile in thePlatform SDK.

dwContext

The context identifier for the file retrieval. See Remarks for more information aboutdwContext.

[cpp] view plain copy print ?
  1. m_pFtpConnection->GetFile("1.txt","D:\\1.txt",false,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY,1)  
删除服务器上的文件

BOOL Remove( LPCTSTR pstrFileName );

Return Value

Nonzero if successful; otherwise 0. If the call fails, the Win32 functionGetLastError may be called to determine the cause of the error.

Parameters

pstrFileName

A pointer to a string containing the file name to remove.

[cpp] view plain copy print ?
  1. m_pFtpConnection->Remove("1.txt")  
上传文件

BOOL PutFile( LPCTSTR pstrLocalFile, LPCTSTRpstrRemoteFile, DWORDdwFlags = FTP_TRANSFER_TYPE_BINARY, DWORDdwContext = 1 );

Return Value

Nonzero if successful; otherwise 0. If the call fails, the Win32 functionGetLastError may be called to determine the cause of the error.

Parameters

pstrLocalFile

A pointer to a string containing the name of the file to send from the local system.

pstrRemoteFile

A pointer to a string containing the name of the file to create on the FTP server.

dwFlags

Specifies the conditions under which the transfer of the file occurs. Can be any of the FTP_TRANSFER_* constants described inOpenFile.

dwContext

The context identifier for placing the file. See Remarks for more information aboutdwContext.

[cpp] view plain copy print ?
  1. m_pFtpConnection->PutFile("D:\\3.txt","3.txt",FTP_TRANSFER_TYPE_BINARY,1) 

 

你可能感兴趣的:(C++)