问题描述
我正在尝试使用批处理文件将文本文件上传到 FTP 服务器.登录成功并显示
I am trying to upload a text file to FTP server using a batch file. It logins successfully and shows
端口命令发送成功
但之后显示
无法打开与端口 xxxx 的数据连接
连接超时
这是批处理脚本:
@echo off
for %%A in (*.csv) do set latest=%%A
echo Latest file is %latest%
echo MYUSERNAME> upload.txt
echo MYPASSWORD>> upload.txt
echo asc>>upload.txt
echo put %latest% s.txt>> upload.txt
echo quit >> upload.txt
ftp -s:upload.txt server58.hostinger.in
推荐答案
这看起来像是 FTP 活动模式的典型问题.服务器无法连接回您的计算机以建立数据传输连接.
This looks like a typical problem with an FTP active mode. The server cannot connect back to your machine to establish a data transfer connection.
这通常会发生,因为现在大多数客户端计算机都位于防火墙或 NAT 或两者后面,这会阻止 FTP 主动模式工作.要使活动模式正常工作,您需要打开防火墙(不推荐)和/或配置 NAT 路由规则.
That typically happens as nowadays most client machines are behind a firewall or NAT or both, what prevents the FTP active mode from working. To make the active mode working you need to open your firewall (not recommended) and/or configure NAT routing rules.
请参阅我关于 FTP 模式和为活动模式配置网络的文章.
See my article on FTP modes and configuring network for an active mode.
或者您使用被动 FTP 模式.不过,Windows ftp.exe
客户端不支持被动模式,这使得它现在变得毫无用处.
Or you use a passive FTP mode. The Windows ftp.exe
client does not support the passive mode though, what makes it pretty useless nowadays.
所以你需要使用另一个命令行 FTP 客户端.大多数 FTP 客户端都支持被动模式.
So you need to use another command-line FTP client. A majority of FTP clients do support the passive mode.
例如,使用 WinSCP 您的批处理文件将如下所示:
For example with WinSCP your batch file would be like:
@echo off
for %%A in (*.csv) do set latest=%%A
echo Latest file is %latest%
winscp.com /command ^
"open ftp://MYUSERNAME:[email protected]/" ^
"put -transfer=ascii %latest% s.txt" ^
"exit"
请注意,WinSCP 默认为被动模式.
Note that WinSCP defaults to the passive mode.
有关详细信息,请参阅 WinSCP 指南:
For details see WinSCP guides for:
(我是 WinSCP 的作者)
这篇关于获取“无法打开到端口 xxxx 的数据连接"从 Windows 批处理文件上传文件到 FTP 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!