0
我是新的android編程。我需要將視頻,jpgs和pdf從ftp服務器下載到我的android設備。Android的ftp下載無法正常下載文件
這裏是一個線程我下載代碼
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");
}
這是我的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;
}
這些代碼dowloads文件,但是當我想打開一些PDF文件或視頻,我得到錯誤「打開文檔時發生錯誤「
如何處理dowload代碼正常工作?什麼可能是錯誤的再現?
P.S:視頻文件100-200 MB,PDF文件1-5 MB
你可以發佈你正在使用的代碼來打開下載文件,也許有關錯誤(堆棧跟蹤)的更多細節? – ben75
不,我試圖打開設備上的文件,而不是代碼。 –
你爲什麼在你的登錄方法中設置它? mFTPClient.setFileType(FTP.ASCII_FILE_TYPE);應該留在最後的二進制... – Martin