我想使用 Apache Commons Net 实现一个仅用于上传数据的 FTP 客户端。 FTP 服务器的连接和登录工作正常。 但是上传不正确。 这些文件与原始文件相比有点大。 而且文件损坏了。 我尝试了一张图片、一段视频和一个文本文件。只有文本文件可以。
现在我在调试的时候看到了
boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
给我 false
。所以不能设置。为什么?
(也许这不是问题所在?)
这是我的其余代码
client=new FTPClient();
try {
int reply;
client.connect(url, port);
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
client.login(user, pw);
boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(300);
client.enterLocalPassiveMode();
if (client.isConnected())
{
try {
File file=new File(<FILE>);
FileInputStream inputStream = new FileInputStream(file);
OutputStream outputStream = client.storeFileStream(file.getName());
byte[] buffer = new byte[4096];
int l;
while((l = inputStream.read(buffer))!=-1)
{
outputStream.write(buffer, 0, l);
}
inputStream.close();
outputStream.flush();
outputStream.close();}
最佳答案
更改以下内容:
boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
应该是:
boolean xxx=client.setFileType(FTP.BINARY_FILE_TYPE);
您混淆了 FileTransferModes 和 FileTypes。
可用的文件类型是:
可用的 FileTransferModes 是:
我想如果 apache 为这些常量类型引入了枚举,那么这种问题就可以避免,但是 java-5 之前的运行时将无法使用该库。
我想知道 java 1.4 兼容性到底有多大问题。
关于java - Apache Commons FTP 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6651158/