[英]Android ftp download doesn't downloads file properly
I'm new on android programming. 我是android编程的新手。 And I need to download videos, jpgs and pdfs from an ftp server to my android device.
而且我需要将视频,jpg和pdf从ftp服务器下载到我的android设备。
Here is my download code in a Thread 这是我在线程中的下载代码
MyFTPClient ftpclient=null;
ftpclient = new MyFTPClient();
status = ftpclient.ftpConnect("xxx.xxx.xxx.xxx", "username", "password", 21);
if (status == true) {
Log.d(TAG, "Connection Success");
FTPFile[] filelist= ftpclient.ftpPrintFilesList("/httpdocs/");
for(int i=0;i<filelist.length;i++){
if(filelist[i].getType()==1)
{
File myFile = new File("/storage/sdcard0/App/"+filelist[i].getName());
if(!myFile.exists()){
File folder=new File("/storage/sdcard0/App/"+filelist[i].getName());
folder.mkdir();
}
}
else if(filelist[i].getType()==0)
{
ftpclient.ftpDownload("/httpdocs/"+filelist[i].getName(), "/storage/sdcard0/App/"+filelist[i].getName());
}
}
else {
Log.d(TAG, "Connection failed");
}
This is my FTP client class 这是我的FTP客户端类
public boolean ftpConnect(String host, String username,
String password, int port)
{
try {
mFTPClient = new FTPClient();
mFTPClient.connect(host, port);
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
boolean status = mFTPClient.login(username, password);
mFTPClient.setFileType(FTP.ASCII_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
return status;
}
} catch(Exception e) {
Log.d(TAG, "Error: could not connect to host " + host+"Cause: "+e.getCause() );
}
return false;
}
public FTPFile[] ftpPrintFilesList(String dir_path)
{
try {
FTPFile[] ftpFiles = mFTPClient.listFiles(dir_path);
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
Log.i(TAG, "File : " + name);
}
else {
Log.i(TAG, "Directory : " + name);
}
}
return ftpFiles;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
public boolean ftpDownload(String srcFilePath, String desFilePath)
{
boolean status = false;
try {
FileOutputStream desFileStream = new FileOutputStream(desFilePath);;
status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
desFileStream.flush();
desFileStream.close();
return status;
} catch (Exception e) {
Log.d(TAG, "download failed");
}
return status;
}
These codes dowloads files but when I want to open some pdfs or videos I get error "An error occured while opening the document" 这些代码会下载文件,但是当我想打开一些pdf或视频时,会出现错误“打开文档时发生错误”
How can I handle the dowload code works properly? 如何处理下载代码正常工作? What can be the reasen of the error?
错误的原因是什么?
PS: The video files are 100-200 MB, pdf files are 1-5 MB PS:视频文件为100-200 MB,pdf文件为1-5 MB
Why are you setting this inside your login method? 为什么在登录方法中设置此设置? mFTPClient.setFileType(FTP.ASCII_FILE_TYPE);
mFTPClient.setFileType(FTP.ASCII_FILE_TYPE); Should be left as binary at the end...
最后应保留为二进制...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.