Apache Commons FTP问题 [英] Apache Commons FTP problems
问题描述
我只想用Apache Commons Net来实现一个FTP客户端来上传数据。
连接和登录到FTP服务器工作正常。
但是上传不起作用。
这些文件与原件一样大小。
文件已损坏。
我尝试了一个图像,一个视频和一个文本文件。
现在我在调试时看到
只有文本文件没有问题。 boolean tmp = client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
给我 false
。所以它不能设置。为什么?
(也许这不是问题?)
这里是我的其他代码
client = new FTPClient();
尝试{
int reply;
client.connect(url,port);
reply = client.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply))
{
client.disconnect();
System.err.println(FTP服务器拒绝连接。);
System.exit(1);
}
client.login(user,pw);
布尔值xxx = client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(300);
client.enterLocalPassiveMode();
$ b $ 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);
应该是:
布尔值xxx = client.setFileType(FTP.BINARY_FILE_TYPE);
您已将FileTransferModes与FileTypes混淆。
可用的文件类型是:
可用的FileTransferModes为:
我想如果apache为这些常量类型引入了枚举,那么可以避免这种问题,但是这个库在java-5之前的运行时不可用。 >
我想知道java 1.4的兼容性究竟有多大。
I want to implement a FTP Client with Apache Commons Net only for uploading data. The Connection and Login to FTP-Server works fine. But the upload does not work right. The files are a little to big as the originals. And the files are damaged. I tried an image, a video and a textfile. Only the textfile is alright.
Now I see while debugging
boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
gives me false
. So it can not be set. Why?
(Maybe this is not the problem?)
Here a the rest of my code
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();}
Change the following:
boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
Should be:
boolean xxx=client.setFileType(FTP.BINARY_FILE_TYPE);
You have confused FileTransferModes with FileTypes.
The available FileTypes are:
The available FileTransferModes are:
I suppose if apache introduced enums for these constant types, then this kind of problem could be avoided, but then the library would not be available to pre-java-5 runtimes.
I wonder how much of an issue java 1.4 compatibility really is.
这篇关于Apache Commons FTP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!