如何将FTP路径添加到我的代码 [英] How To Add FTP Path to My Code

查看:82
本文介绍了如何将FTP路径添加到我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个用于将FTP文件传输到服务器的简单代码.

 #include   ><   windows.h  > 
 #include   <   wininet.h  > 
 #include   <   winnt.h  > 

使用 命名空间 std;

 int  main(){
    //  LPCTSTR服务器= _T("ftp.server.com/public_html/data"); 
    LPCTSTR服务器= _T(" );


INTERNET_PORT端口= INTERNET_DEFAULT_FTP_PORT;
LPCTSTR用户= _T(" );
LPCTSTR pass = _T(" );
DWORD服务= INTERNET_SERVICE_FTP;
DWORD context =  0 ;
LPCTSTR szLocalFile = _T(" );
LPCTSTR szRemoteFile = _T(" );


    HINTERNET hSession = InternetOpen( 0 ,INTERNET_OPEN_TYPE_PRECONFIG, 0  0  0 );
    HINTERNET hService = InternetConnect(hSession,服务器,INTERNET_DEFAULT_FTP_PORT,用户,密码,INTERNET_SERVICE_FTP, 0  0 );
    FtpPutFile(hService,szLocalFile,szLocalFile,FTP_TRANSFER_TYPE_BINARY, 0 );
    如果(((FtpPutFile(hService,szLocalFile,szRemoteFile,FTP_TRANSFER_TYPE_BINARY, 0 ))== "   false";
    InternetCloseHandle(hService);
    InternetCloseHandle(hSession);
    cout<< " ;
   
    返回  0 ;
} 



当我使用server ="www.server.com"时,它可以正常工作,但我的ftp地址也是包含路径.
当我尝试执行server ="ftp.server.com/public_html/data"时不起作用,也不会传输任何文件.
如何将我的FTP路径添加到此代码中以获取传输代码?
此致

解决方案

使用 FtpSetCurrentDirectory [ FtpPutFile(hService,szLocalFile,szLocalFile,FTP_TRANSFER_TYPE_BINARY, 0 ); if (((FtpPutFile(hService,szLocalFile,szRemoteFile,FTP_TRANSFER_TYPE_BINARY, 0 ))== 错误)cout<< " ;


与:

 如果(!FtpPutFile(hService,szLocalFile,szRemoteFile,FTP_TRANSFER_TYPE_BINARY, 0  ))
cout<< " ; 


hi,
I have one simple code for FTP files to my server.

#include <windows.h>
#include <wininet.h>
#include <winnt.h>

using namespace std;

int main() {
    //LPCTSTR server = _T("ftp.server.com/public_html/data");
    LPCTSTR server = _T("www.server.com");


INTERNET_PORT port = INTERNET_DEFAULT_FTP_PORT;
LPCTSTR user = _T("user");
LPCTSTR pass = _T("pass");
DWORD service = INTERNET_SERVICE_FTP;
DWORD context = 0;
LPCTSTR szLocalFile = _T("C:\\ss.csv");
LPCTSTR szRemoteFile = _T("ss.csv");


    HINTERNET hSession = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
    HINTERNET hService = InternetConnect(hSession, server, INTERNET_DEFAULT_FTP_PORT, user, pass, INTERNET_SERVICE_FTP, 0, 0);
    FtpPutFile(hService, szLocalFile, szLocalFile, FTP_TRANSFER_TYPE_BINARY, 0);
    if((FtpPutFile(hService, szLocalFile, szRemoteFile, FTP_TRANSFER_TYPE_BINARY, 0))==false) cout<<"false";
    InternetCloseHandle(hService);
    InternetCloseHandle(hSession);
    cout<<"Finished";
   
    return 0;
}



When I am using server="www.server.com" it works correctly but my ftp address is include path too.
When I try do server="ftp.server.com/public_html/data" not works and no transfer any file.
How to add my FTP path to this code for transfer code?
Regards,

With InternetConnect[^] you are only supposed to specify the server name you want to connect to. After making the connection use FtpSetCurrentDirectory[^] to change the folder on the FTP server.

By the way please note that you are uploading the file two times.
Replace:

FtpPutFile(hService, szLocalFile, szLocalFile, FTP_TRANSFER_TYPE_BINARY, 0);
if((FtpPutFile(hService, szLocalFile, szRemoteFile, FTP_TRANSFER_TYPE_BINARY, 0))==false) cout<<"false";


with:

if( !FtpPutFile(hService, szLocalFile, szRemoteFile, FTP_TRANSFER_TYPE_BINARY, 0) )
	cout<<"false";


这篇关于如何将FTP路径添加到我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆