SocketException:FTP上传时重置连接

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

我正在尝试将文件上传到FTP服务器。这是我的代码:

FTPClient ftpClient = new FTPClient();
try {
    List<PoiCmsRule> appPois = getAllRules();
    try (FileWriter writer = new FileWriter(backupFile)) {
        new Gson().toJson(backupData, writer);
    }
    try (FileInputStream fis = new FileInputStream(backupFile)) {
        ftpClient.connect(host);
        ftpClient.login(login, password);
        ftpClient.storeFile("backup.json", fis);
        ftpClient.logout();
    }
} catch (Exception e) {
    log.error(e.getMessage(), e);
} finally {
    try {
        ftpClient.disconnect();
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
}

而且当我在localhost上运行时,它工作得很好。但是当我在服务器上运行时,出现此异常:

Caused by: java.net.SocketException: Connection reset
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:186)
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
    at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)

我认为这表明我的防火墙或代理可能有问题。

这是一个使用Nginx作为代理在Ubuntu服务器上运行的Spring Boot应用程序。

我如何找到导致此异常的原因?


从被动模式下与命令行ftp成功连接的日志:

ubuntu@ip-server:~$ ftp -p -d my_host
Connected to my_host
220 FTP-Server
ftp: setsockopt: Bad file descriptor
Name (my_host): username
---> USER username
331 Please specify the password.
Password:
---> PASS XXXX
230 Login successful.
---> SYST
215 UNIX Type: L8
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
ftp: setsockopt (ignored): Permission denied
---> PASV
227 Entering Passive Mode (153,92,207,128,127,65)
---> LIST
150 Here comes the directory listing.
// file list
226 Directory send OK.
ftp>
java sockets ubuntu ftp apache-commons-net
1个回答
1
投票

Apache Commons Net FTPClient默认为FTP 活动模式。如今,由于存在无处不在的防火墙和NAT,主动模式几乎无法工作(有关详细信息,请参阅我在FTP active/passive connection modes上的文章)。

要将FTPClient切换到被动模式,请在FTPClient.enterLocalPassiveMode之后的某个地方调用FTPClient.enterLocalPassiveMode

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