gpt4 book ai didi

Java FTPClient (apache commons) storefile() 成功上传文件,然后超时..?

转载 作者:搜寻专家 更新时间:2023-11-01 03:23:11 29 4
gpt4 key购买 nike

这是我必须将文件上传到 FTP 服务器的一些代码:

private boolean ftpSend(byte[] fileBytes, String filename, HashMap<String, String> endpointAddressData) throws Exception {
boolean success = false;

String login = endpointAddressData.get("user");
String hostname = endpointAddressData.get("host");
String password = endpointAddressData.get("password");

String portString = endpointAddressData.get("port");
int port = (portString == null) ? 21 : Integer.parseInt(portString);

FTPClient ftpClient = new FTPClient();
ByteArrayInputStream inputStream = null;
try {
ftpClient.setConnectTimeout(10000);
ftpClient.connect(hostname, port);

int reply = ftpClient.getReplyCode();

if(!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();

throw new Exception("FTP server " + hostname + " refused connection.");
}

if (password != null) {
ftpClient.login(login, password);
} else {
ftpClient.user(login);
}

logger_.debug("FTP - Writing: " + ftpClient.printWorkingDirectory() + "/" + filename + " on host " + endpointAddressData.get("host"));

inputStream = new ByteArrayInputStream(fileBytes);
success = ftpClient.storeFile(filename, inputStream);
inputStream.close();
success = success && ftpClient.completePendingCommand();

logger_.debug("FTP - Completed: " + ftpClient.printWorkingDirectory() + "/" + filename + " on host " + endpointAddressData.get("host"));

ftpClient.disconnect();

} catch (Exception ex) {
success = false;

logger_.error("An error occurred during FTP upload to " + endpointAddressData.get("host") + ".", ex);
} finally {
if (ftpClient.isConnected()) {
ftpClient.abort();
ftpClient.disconnect();
}

if (inputStream != null) {
inputStream.close();
}

return success;
}
}

当我用一些测试数据运行它时,上传显然成功完成(这里是 FTP 服务器日志):

(000009)01/05/2014 12:13:09 PM - (not logged in) (127.0.0.1)> USER ######
(000009)01/05/2014 12:13:09 PM - (not logged in) (127.0.0.1)> 331 Password required for ######
(000009)01/05/2014 12:13:09 PM - (not logged in) (127.0.0.1)> PASS ########
(000009)01/05/2014 12:13:09 PM - anonymous (127.0.0.1)> 230 Logged on
(000009)01/05/2014 12:13:09 PM - anonymous (127.0.0.1)> PWD ########
(000009)01/05/2014 12:13:09 PM - anonymous (127.0.0.1)> 257 "/" is current directory.
(000009)01/05/2014 12:13:09 PM - anonymous (127.0.0.1)> PORT 127,0,0,1,250,242
(000009)01/05/2014 12:13:09 PM - anonymous (127.0.0.1)> 200 Port command successful
(000009)01/05/2014 12:13:09 PM - anonymous (127.0.0.1)> STOR testFTP1.txt
(000009)01/05/2014 12:13:09 PM - anonymous (127.0.0.1)> 150 Opening data channel for file upload to server of "/testFTP1.txt"
(000009)01/05/2014 12:13:09 PM - anonymous (127.0.0.1)> 226 Successfully transferred "/testFTP1.txt"
(000009)01/05/2014 12:15:10 PM - anonymous (127.0.0.1)> 421 Connection timed out.
(000009)01/05/2014 12:15:10 PM - anonymous (127.0.0.1)> disconnected.

我做错了什么?

最佳答案

在调试器中单步执行后,我发现 ftpClient.completePendingCommand() 调用导致了超时。我猜命令已经完成,它正在无限期地等待下一个命令。我注释掉了那一行,代码很容易完成。

来自 FTPClient 上的 JavaDoc: http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#completePendingCommand()

There are a few FTPClient methods that do not complete the entire sequence of FTP commands to complete a transaction. These commands require some action by the programmer after the reception of a positive intermediate command. After the programmer's code completes its actions, it must call this method to receive the completion reply from the server and verify the success of the entire transaction.

我想 storeFile()(FTP STOR 命令)不是其中一种方法。

关于Java FTPClient (apache commons) storefile() 成功上传文件,然后超时..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23414892/

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