我正在尝试将文件从FileZilla Server下载到Android设备。 一切似乎都很严格,与FTP的连接,登录,文件存在以及下载的响应都可以。 但是当我转到Android设备中的文件时,它只有64KB的大小,它必须是因为已损坏而无法打开...我已经尝试了另一个49KB的文件并下载48KB并且它也腐败了。我使用Adobe Acrobat Reader和QuickOffice打开文件。
String usuarioLogin = params[0];
FTPClient ftp = null;
String LOG_TAG = "FTP";
ftp = new FTPClient();
String server = params[0];
int portNumber = Integer.parseInt(params[1]);
String user = params[2];
String password = params[3];
String filename = params[4];
File localFile = UtilsVictor.crearFichero(params[5]);
ftp.connect(server, portNumber);
Log.d(LOG_TAG, "Connected. Reply: " + ftp.getReplyString());
ftp.enterLocalPassiveMode();
ftp.login(user, password);
Log.d(LOG_TAG, "Logged in");
ftp.setFileType(FTP.LOCAL_FILE_TYPE);
Log.d(LOG_TAG, "Downloading");
OutputStream outputStream = null;
boolean success = false;
outputStream = new BufferedOutputStream(new FileOutputStream(
localFile));
success = ftp.retrieveFile(filename, outputStream);
if(success) {
Log.d(LOG_TAG, "success!!");
} else{
Log.d(LOG_TAG, "NOOOOT success!!");
}
FileZilla服务器的日志:
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> Connected on port 50000, sending welcome message...
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 220-FileZilla Server 0.9.60 beta
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 220 Please visit https://filezilla-project.org/
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> USER VICTOR
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 331 Password required for victor
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> PASS ****
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 230 Logged on
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> TYPE I
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 200 Type set to I
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> PASV
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 227 Entering Passive Mode (192,168,1,41,125,49)
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> RETR /Informes/572760344M/aaa.pdf
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 150 Opening data channel for file download from server of "/Informes/572760344M/aaa.pdf"
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 226 Successfully transferred "/Informes/572760344M/aaa.pdf"
(000019)13/01/2018 17:13:48 - victor (192.168.1.38)> disconnected.
问题出在哪里?
答案 0 :(得分:1)
你错过了
outputStream.close();
无需完全重写代码。您的new code只是不必要的重新实现,FTPClient.retrieveFile
在内部做了什么。
即使您选择使用FTPClient.retrieveFileStream
,也请使用Util.copyStream
而不是自己实施复制循环。
答案 1 :(得分:0)
我创建了错误,它是在存储过程中:
更改此内容:
OutputStream outputStream = null;
boolean success = false;
outputStream = new BufferedOutputStream(new FileOutputStream(
localFile));
success = ftp.retrieveFile(filename, outputStream);
if(success) {
Log.d(LOG_TAG, "success!!");
} else{
Log.d(LOG_TAG, "NOOOOT success!!");
}
对此:
InputStream myFileStream = null;
myFileStream = ftp.retrieveFileStream(filename);
File fileToWrite = new File(params[5]);
java.io.FileOutputStream fos = new java.io.FileOutputStream("/storage/emulated/0/Download/"+fileToWrite);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte data[] = new byte[1024];
int x =0;
while((x=myFileStream.read(data,0,1024))>=0){
bout.write(data,0,x);
}
bout.close();
myFileStream.close();
res = ftp.getReplyCode();
完美无缺!