- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是我必须将文件上传到 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/
自从我不得不从 java ftp 文件以来已经有很多年了,所以我对这种可能性有点脱节。我找到的文档列出了我使用的两种可能性。标准 Sun ftp 实现和 Apache 的 Jakarta ftp 解决
我尝试在我的应用程序中使用 ftp 客户端 第一个我使用org.apache.commons.net.ftp.FTPClient 但在Linux服务器上运行时 可以登录ftp服务器,但无法获取文件列表
我的 ftp 位置有 3 个不同大小的文件,分别是 2KB、76KB 和 216MB。我必须一个接一个地处理这些文件,我可以使用retrieveFileStream(FILE_NAME_HERE)轻松
FTPClient.changeWorkingDirectory & changeToParentDirectory()。如何更改 ftpCient 中的目录。在资源管理器中,ftp 路径如下所示 f
我有一个给定的 FTP 服务器。我可以通过 WinSCP 和其他程序连接到服务器。我已经通过 Java 连接到服务器,但过了一会儿我无法连接。根据 WinSCP,我可以一直连接。现在的问题是服务器是问
我正在尝试从 ftp 服务器下载文件,但在检索文件时卡住了。我正在使用 commons-net-3.6.jar 我注意到的事情 当我使用 ftpClient.enterRemotePassiveMod
我想从 Android 应用程序上传文件到 FTP 服务器。我为此使用 ftp4j.jar,因为它具有附加功能,可以附加到服务器上已有的文件。以前工作正常,但现在它已停止工作附加文件。其余正在工作。我
我正在制作一个应用程序,可让您一次将最多 5 个文件上传到服务器。我正在使用 apache FTPClient 和 Filezilla 服务器。当我一次上传一个文件时,它可以工作,但是当我尝试进行 2
大家好,我正在尝试上传一个小文件,如 4mb 或其他带有 apache commons ftpclient 的文件,但它没有上传我的文件,没有给我任何错误,并说上传成功这是我的代码,请提前帮助我谢谢.
我正在使用 Apache Commons net FTPClient 登录 FTP 服务器并读取文件。我成功登录,并且可以看到它登录成功,因为它在标题字符串中显示了工作目录路径。但是,当我使用 lis
我试图从 ftp 服务器获取列表并且有编码问题 FTPClient.listfiles(String path) 方法 如果路径包含非拉丁字符,它总是返回空数组。 (我也在 unicode 中使用带有
我制作了一个小应用程序,可以将文件上传到 FTP 服务器。问题是我在方法中使用了被动模式 enterLocalPassiveMode() 最近有人告诉我在 FTP 服务器上不允许使用被动模式,所以我应
我写了一个从 FTP 服务器下载文件的 java 类。一切看起来都很好,但是当我检查文件大小时,它们比原始文件小一点。 知道为什么会这样吗? import java.io.FileOutputStre
我正在使用 Apache FTPClient 获取文件和子目录文件列表。但它无法从带空格的目录名称中获取文件列表。这是一个示例 - 我尝试了两个不同的目录: FTPClient client
我是 android 和 java 编程的新手。我写了下面的代码来连接到 ftp 服务器 public class NewMainActivity extends Activity { TextVie
1、Ubuntu 安装 Vsftpd 服务 1、安装 sudo apt-get install vsftpd 2、添加用户(uftp) sudo useradd -d /home/
我有一个本地 FTP 服务器(使用 FileZilla 服务器创建),并且 XLS 文件很少。我需要将文件输入流返回到我的 Java 应用程序,以便我可以解析数据等。 为此,我尝试使用 FTPClie
我尝试使用 FTPClient 类进行连接。这段代码运行完美: client.connect("172.20.5.131"); 但是当 IP 地址类似于 -1406991772 时,我无法连接。我怎样
我正在使用 FTPClient (org.apache.commons.net.ftp.FTPClient) 将一些文件上传到 FTP 服务器。 我需要检查父目录是否存在;并根据需要创建它们
我必须上传一个zip文件到ftp服务器,这里的zip文件也是动态构建的。 import java.io.File; import java.io.FileInputStream; import jav
我是一名优秀的程序员,十分优秀!