简体   繁体   English

从Windows批处理文件将文件上传到FTP时,出现“无法打开与端口xxxx的数据连接”

[英]Getting “Could not open data connection to port xxxx” when uploading file to FTP from Windows batch file

I am trying to upload a text file to FTP server using a batch file. 我正在尝试使用批处理文件将文本文件上传到FTP服务器。 It logins successfully and shows 它成功登录并显示

Port command sent successfully 端口命令发送成功

but after that shows 但之后显示

Could not open data connection to port xxxx 无法打开与端口xxxx的数据连接
Connection timed out 连接超时

This is the batch script: 这是批处理脚本:

@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

This looks like a typical problem with an FTP active mode. 这看起来像是FTP活动模式的典型问题。 The server cannot connect back to your machine to establish a data transfer connection. 服务器无法连接回您的计算机以建立数据传输连接。

That typically happens as nowadays most client machines are behind a firewall or NAT or both, what prevents the FTP active mode from working. 这种情况通常发生在当今大多数客户端计算机位于防火墙或NAT或同时位于两者之后的情况下,这导致FTP主动模式无法正常工作。 To make the active mode working you need to open your firewall (not recommended) and/or configure NAT routing rules. 要使活动模式正常工作,您需要打开防火墙(不推荐)和/或配置NAT路由规则。

See my article on FTP modes and configuring network for an active mode . 请参阅我关于FTP模式和将网络配置为活动模式的文章


Or you use a passive FTP mode. 或者您使用被动FTP模式。 The Windows ftp.exe client does not support the passive mode though, what makes it pretty useless nowadays. 但是,Windows ftp.exe客户端不支持被动模式,这使它在当今非常无用。

So you need to use another command-line FTP client. 因此,您需要使用另一个命令行FTP客户端。 A majority of FTP clients do support the passive mode. 大多数FTP客户端都支持被动模式。

For example with WinSCP your batch file would be like: 例如,使用WinSCP,您的批处理文件将如下所示:

@echo off

for %%A in (*.csv) do set latest=%%A 
echo Latest file is %latest%

winscp.com /command ^
    "open ftp://MYUSERNAME:MYPASSWORD@server58.hostinger.in/" ^
    "put -transfer=ascii %latest% s.txt" ^
    "exit"

Note that WinSCP defaults to the passive mode. 请注意,WinSCP默认为被动模式。

For details see WinSCP guides for: 有关详细信息,请参见WinSCP指南:

(I'm the author of WinSCP) (我是WinSCP的作者)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM