| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5054 人关注过本帖, 1 人收藏
标题:从FTP服务器上下载文件失败,请帮忙分析?
只看楼主 加入收藏
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
结帖率:96.03%
收藏(1)
已结贴  问题点数:20 回复次数:25 
从FTP服务器上下载文件失败,请帮忙分析?
* How to download a file from the FTP server using FtpGetFile
* 使用 FtpGetFile 从 FTP 上下载文件
*--------------------------------------------------------------
Clea
* lAccessType - some values
#Define INTERNET_INVALID_PORT_NUMBER  0
#Define INTERNET_OPEN_TYPE_DIRECT     1
#Define INTERNET_OPEN_TYPE_PROXY      3
#Define INTERNET_DEFAULT_FTP_PORT     21

* lFlags: only a few
#Define INTERNET_FLAG_ASYNC             268435456     && &H10000000
#Define INTERNET_FLAG_FROM_CACHE        16777216    && &H1000000
#Define INTERNET_FLAG_OFFLINE           16777216
#Define INTERNET_FLAG_CACHE_IF_NET_FAIL 65536        && &H10000

* registry access settings
#Define INTERNET_OPEN_TYPE_PRECONFIG  0
#Define FTP_TRANSFER_TYPE_ASCII       1
#Define FTP_TRANSFER_TYPE_BINARY      2

* type of service to access
#Define INTERNET_SERVICE_FTP     1
#Define INTERNET_SERVICE_GOPHER  2
#Define INTERNET_SERVICE_HTTP    3

* file attributes
#Define FILE_ATTRIBUTE_NORMAL    128    && 0x00000080

    Declare INTEGER InternetOpen IN wininet;
        STRING sAgent, INTEGER lAccessType, STRING sProxyName,;
        STRING sProxyBypass, STRING  lFlags

    Declare INTEGER InternetCloseHandle IN wininet INTEGER hInet

    Declare INTEGER InternetConnect IN wininet;
        INTEGER hInternetSession, STRING  sServerName,;
        INTEGER nServerPort, STRING  sUsername,;
        STRING  sPassword, INTEGER lService,;
        INTEGER lFlags, INTEGER lContext

    Declare INTEGER FtpGetFile IN wininet;
        INTEGER hFtpSession, STRING  lpszRemoteFile,;
        STRING  lpszNewFile, INTEGER fFailIfExists,;
        INTEGER dwFlagsAndAttributes,;
        INTEGER dwFlags, INTEGER dwContext

* you are free to choose any name, say "John Dow"
* server-side variable $AGENT is the target
sAgent = "hongyu"

sProxyName = Chr(0)        && I have no proxy
sProxyBypass = Chr(0)    && so there is nothing to bypass
lFlags = 0                && no flags used

* initialize access to Inet functions
hOpen = InternetOpen (sAgent, INTERNET_OPEN_TYPE_DIRECT,;
    sProxyName, sProxyBypass, lFlags)

If hOpen = 0
    ? "找不到库 WinInet.Dll 的入口"
    Return
Else
    ? "Wininet 句柄: " + LTRIM(STR(hOpen))
Endif

* connection parameters - you better put your data
strHost = "hz230674.ftp.
strUser = "hz230674"
strPwd  = "*****"

* connecting to the FTP
hFtpSession = InternetConnect (hOpen, strHost,;
    INTERNET_INVALID_PORT_NUMBER,;
    strUser, strPwd,;
    INTERNET_SERVICE_FTP, 0, 0)

If hFtpSession = 0
    * close access to Inet functions and exit
    = InternetCloseHandle (hOpen)
    ? "FTP " + strHost + " 无效"
    Return
Else
    ? "连接到 " + strHost + " as: [" + strUser + ", *****]"
    ? "FTP 连接句柄: " + LTRIM(STR(hFtpSession))
Endif

* downloading a file from the FTP
* no check whether the target file exists
lpszRemoteFile = "/htdocs/images/mylove2.txt"           && if it still exists
lpszNewFile    = "D:\myfolder"    && check the destination folder
fFailIfExists  = 0        && do not stop if the target already exists
dwContext      = 0

lnResult = FtpGetFile (hFtpSession, lpszRemoteFile, lpszNewFile,;
    fFailIfExists, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_ASCII,;
    dwContext)

If lnResult # 1
    ? "下载文件失败!"
Endif

* close handles
= InternetCloseHandle (hFtpSession)
= InternetCloseHandle (hOpen)
Return        && main

[此贴子已经被作者于2017-8-31 20:37编辑过]

搜索更多相关主题的帖子: FTP the Define INTEGER STRING 
2017-08-31 16:08
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2017-08-31 17:02
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
收藏
得分:0 
回复 2楼 吹水佬

****** 代码重新整理如下:*******

* How to download a file from the FTP server using FtpGetFile
* 使用 FtpGetFile 从 FTP 上下载文件
*--------------------------------------------------------------
Clea
* lAccessType - some values
#Define INTERNET_INVALID_PORT_NUMBER  0
#Define INTERNET_OPEN_TYPE_DIRECT     1
#Define INTERNET_OPEN_TYPE_PROXY      3
#Define INTERNET_DEFAULT_FTP_PORT     21

* lFlags: only a few
#Define INTERNET_FLAG_ASYNC             268435456     && &H10000000
#Define INTERNET_FLAG_FROM_CACHE        16777216    && &H1000000
#Define INTERNET_FLAG_OFFLINE           16777216
#Define INTERNET_FLAG_CACHE_IF_NET_FAIL 65536        && &H10000

* registry access settings
#Define INTERNET_OPEN_TYPE_PRECONFIG  0
#Define FTP_TRANSFER_TYPE_ASCII       1
#Define FTP_TRANSFER_TYPE_BINARY      2

* type of service to access
#Define INTERNET_SERVICE_FTP     1
#Define INTERNET_SERVICE_GOPHER  2
#Define INTERNET_SERVICE_HTTP    3

* file attributes
#Define FILE_ATTRIBUTE_NORMAL    128    && 0x00000080

    Declare INTEGER InternetOpen IN wininet;
        STRING sAgent, INTEGER lAccessType, STRING sProxyName,;
        STRING sProxyBypass, STRING  lFlags

    Declare INTEGER InternetCloseHandle IN wininet INTEGER hInet

    Declare INTEGER InternetConnect IN wininet;
        INTEGER hInternetSession, STRING  sServerName,;
        INTEGER nServerPort, STRING  sUsername,;
        STRING  sPassword, INTEGER lService,;
        INTEGER lFlags, INTEGER lContext

    Declare INTEGER FtpGetFile IN wininet;
        INTEGER hFtpSession, STRING  lpszRemoteFile,;
        STRING  lpszNewFile, INTEGER fFailIfExists,;
        INTEGER dwFlagsAndAttributes,;
        INTEGER dwFlags, INTEGER dwContext

* you are free to choose any name, say "John Dow"
* server-side variable $AGENT is the target
sAgent = "hongyu"

sProxyName = Chr(0)        && I have no proxy
sProxyBypass = Chr(0)    && so there is nothing to bypass
lFlags = 0                && no flags used

* initialize access to Inet functions
hOpen = InternetOpen (sAgent, INTERNET_OPEN_TYPE_DIRECT,;
    sProxyName, sProxyBypass, lFlags)

If hOpen = 0
    ? "找不到库 WinInet.Dll 的入口"
    Return
Else
    ? "Wininet 句柄: " + LTRIM(STR(hOpen))
Endif

* connection parameters - you better put your data
strHost = "hz230674.ftp.
strUser = "hz230674"
strPwd  = "******"

* connecting to the FTP
hFtpSession = InternetConnect (hOpen, strHost,;
    INTERNET_INVALID_PORT_NUMBER,;
    strUser, strPwd,;
    INTERNET_SERVICE_FTP, 0, 0)

If hFtpSession = 0
    * close access to Inet functions and exit
    = InternetCloseHandle (hOpen)
    ? "FTP " + strHost + " 无效"
    Return
Else
    ? "连接到 " + strHost + " as: [" + strUser + ", *****]"
    ? "FTP 连接句柄: " + LTRIM(STR(hFtpSession))
Endif

* downloading a file from the FTP
* no check whether the target file exists
lpszRemoteFile = "/htdocs/images/mylove2.txt"           && if it still exists
lpszNewFile    = "D:\myfolder"    && check the destination folder
fFailIfExists  = 0        && do not stop if the target already exists
dwContext      = 0

lnResult = FtpGetFile (hFtpSession, lpszRemoteFile, lpszNewFile,;
    fFailIfExists, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_ASCII,;
    dwContext)

If lnResult # 1
    ? "下载文件失败!"
Endif

* close handles
= InternetCloseHandle (hFtpSession)
= InternetCloseHandle (hOpen)
Return        && main





[此贴子已经被作者于2017-8-31 20:34编辑过]

2017-08-31 20:32
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
收藏
得分:0 
回复 2楼 吹水佬
服务器密码已发送给您,拜托您测试一下。
2017-08-31 20:41
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:20 
以下是引用su0527在2017-8-31 20:41:42的发言:

服务器密码已发送给您,拜托您测试一下。

运行未见异常
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

2017-08-31 21:02
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:0 
防火墙可能会有影响
2017-08-31 21:10
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
收藏
得分:0 
我的电脑 D:\myfolder 中并没有mylove2文件。
2017-08-31 21:29
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
收藏
得分:0 
回复 6楼 吹水佬
D:\myfolder 中没有mylove2 文件。

图片附件: 游客没有浏览图片的权限,请 登录注册
2017-08-31 21:34
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:0 
是D盘根目录下一个 myfolder 文件,用记事本打开myfolder文件只有一句“我爱你”。
2017-08-31 21:39
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:0 
我第一次运行时防火墙警告是否允许,之后未见其他问题。
2017-08-31 21:41
快速回复:从FTP服务器上下载文件失败,请帮忙分析?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.083007 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved