gpt4 book ai didi

java - FTPSClientretrievefile() 挂起

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:30 25 4
gpt4 key购买 nike

我正在创建一个 apache FTPS 客户端(因为远程服务器不允许普通 FTP)。我可以毫无问题地连接和删除文件,但是当使用retrieveFile() 或retrieveFileStream() 时,它会挂起。

由于某种原因,非常小的文件确实会传输(最多 5792 字节),但其他任何文件都会提供以下 PrintCommandListener 输出:

run:
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 2 of 50 allowed.
220-Local time is now 19:42. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
AUTH TLS
234 AUTH TLS OK.
USER
331 User OK. Password required
PASS
230 OK. Current restricted directory is /
TYPE A
200 TYPE is now ASCII
EPSV
229 Extended Passive mode OK (|||53360|)
RETR test.txt
150-Accepted data connection
150 7.3 kbytes to download

这是代码:

try {

FTPSClient ftpClient = new FTPSClient("tls",false);

ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

ftpClient.connect(host, port);

int reply = ftpClient.getReplyCode();

if (FTPReply.isPositiveCompletion(reply)) {
ftpClient.enterLocalPassiveMode();
ftpClient.login(username, password);
ftpClient.enterLocalPassiveMode();
FileOutputStream outputStream = new FileOutputStream(tempfile);
ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
ftpClient.retrieveFile("test.txt", outputStream);
outputStream.close();
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ioe) {
System.out.println("FTP client received network error");
}

任何想法都将不胜感激。

最佳答案

通常,FTPS 连接的 FTP 命令序列为(根据 RFC 4217)AUTH TLSPBSZ 0then USERPASS。因此你可以尝试:

FTPSClient ftpClient = new FTPSClient("tls",false);
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
ftpClient.execPBSZ(0);
reply = ftpClient.getReplyCode();
// Check for PBSZ error responses...
ftpClient.execPROT("P");
reply = ftpClient.getReplyCode();
// Check for PROT error responses...

ftpClient.enterLocalPassiveMode();

明确告诉服务器缓冲数据连接(PBSZ 0),并使用TLS来保护数据传输(PROT P)。

事实上,您能够传输一些字节,这表明该问题不是防火墙/路由器/NAT 的常见问题,这是另一个常见的 FTPS 问题。

希望这有帮助!

关于java - FTPSClientretrievefile() 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36581727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com