gpt4 book ai didi

java - FTP 在 5 分钟不活动后不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:27 25 4
gpt4 key购买 nike

我有一个进行模拟的应用程序。在每次模拟开始时,都会创建 FTP 连接,然后完成一些上传/下载文件操作。然后开始仿真。仿真结束后,再次进行一些上传文件操作,备份仿真相关文件夹

如果模拟时间超过 5 分钟,我们会遇到问题。与此同时,FTP 连接空闲。我会遇到以下异常。

java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:276)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:122)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:212)
at java.io.BufferedWriter.flush(BufferedWriter.java:236)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:477)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:537)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:586)
at org.apache.commons.net.ftp.FTP.quit(FTP.java:794)
at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:788)


Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:295)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:495)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:537)
at org.apache.commons.net.ftp.FTP.mkd(FTP.java:1363)
at org.apache.commons.net.ftp.FTPClient.makeDirectory(FTPClient.java:1971)

我在模拟完成后尝试了以下操作,我尝试重新初始化 FTP,但没有成功。我看到 FTP 端的连接是 Activity 的

1)FTPClient.isConnected()方法返回true

2)当我调用 FTPClient.connect() 时,它抛出异常 FTP is already initialized

3)我尝试在 FTP 的连接方法中设置超时。

public boolean connect() throws IllegalStateException, FileTransferException {
/*
* Precondition checks
*/
final FTPClient clientBefore = this.getClient();
if (clientBefore != null && clientBefore.isConnected()) {
throw new IllegalStateException("FTP Client is already initialized");
}
// Create the client
final FTPClient client = new FTPClient();
client.setConnectTimeout(600000);
client.setDefaultTimeout(600000);
// FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
// client.configure(conf);
log.trace("Connecting to FTP Server at " + config.getCanonicalForm());
try {
client.connect(config.getHost(), config.getPort());
client.setSoTimeout(600000);
} catch (final IOException ioe) {
throw new FileTransferException("Error in connecting to " + config.getCanonicalForm(),
ioe);
}
log.trace("Connected to FTP Server at: " + config.getCanonicalForm());
this.setClient(client);
// Check that the last operation succeeded
this.checkLastOperation();
boolean result;
try {
// Login
if (client.login(config.getUserName(), config.getPassword())) {
result = true;
// If there's a pwd defined, cd into it.
final String pwd = this.getPresentWorkingDirectory();
if (pwd != null) {
this.cd(pwd);
}
} else {
result = false;
}
} catch (final Exception e) {
throw new FileTransferException("Could not log in", e);
}
return result;
}

现在,我陷入了僵局,我不知道是什么问题

最佳答案

如果您真的必须保持连接有效,请对服务器运行 NOOP 命令,以便控制连接保持有效。

要么手动执行:

client.sendNoOp();

或者设置客户端以固定的间隔速率执行:

client.setControlKeepAliveTimeout(300); // set timeout to 5 minutes

但如果你能避免浪费资源,logoutdisconect在初始下载阶段之后,进行本地处理和 connect/login之后再次上传。

关于java - FTP 在 5 分钟不活动后不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17773317/

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