尝试上传文件时,FTPClient连接超时

问题描述 投票:0回答:1

我正在使用org.apache.commons.net.ftp.FTPClient将文件上传到Windows FTP服务器。

我在将文件上传到FTP服务器时遇到了问题(还有没有内容的文件)。我收到Connection timed out (Connection timed out)

很奇怪,我能够创建文件夹。

这是我的代码的样子:

public FtpClient getFtpClient() throws ConnectException {
    FTPClient ftpClient = new FTPClient();
    try {

        ftpClient.connect("localhost", 21);
        ftpClient.enterLocalPassiveMode();

        ftpClient.login("username", "password");

        final int loginResponse = ftpClient.getReplyCode();

        if (!FTPReply.isPositiveCompletion(loginResponse)) {
            ftpClient.disconnect();
        }

        ftpClient.setFileType(BINARY_FILE_TYPE);

        ftpClient.setRemoteVerificationEnabled(false);

    } catch (final IOException e) {}

    return ftpClient;
}


private void connectAndSend(String message) throws FileTransferException {
    try ((final FTPClient ftpClient = getFtpClient())) {
        // code to create dir if they do not exist goes here
        final ByteArrayInputStream inputStream = new ByteArrayInputStream(message.getBytes());
      ftpClient.storeFile("c:\\upload\\folderA\\subfolderB\\2019-10-14-20.11.54.111.fileName.dat", inputStream);
    } catch (IOException e) {}
}

为什么可以创建文件夹,但是当我尝试上传文件时却得到Connection timed out (Connection timed out)

我尝试过ftpClient.enterLocalPassiveMode();有无,也没有运气。

而且,我还可以使用FileZilla将相同的文件上传到FTP服务器。我试过没有运气的空文件。我可以使用FileZilla进行上传,但不能通过Java程序进行上传。

Java程序位于EC2实例中运行的docker映像中。 FileZilla在Windows计算机上运行。所有计算机(EC2实例,FileZilla和FTP服务器)都位于不同的位置。

java ftp ftp-client
1个回答
0
投票

FTPClient.storeFile的第一个参数是:

FTPClient.storeFile-给出远程文件的名称。

我不认为remote是您的FTP服务器的有效远程路径。使用在使用FTP客户端登录时看到的路径。

© www.soinside.com 2019 - 2024. All rights reserved.