FTP サーバーに接続し、FTP サーバーでホストされているビデオ ファイルをダウンロードし、SD カード内のフォルダーに保存する Android アプリを開発しています。そのために、「org.apache.commons.net.ftp.FTPClient」ライブラリを使用しています。サーバーに接続してアプリからサーバー上のファイル名のリストを取得できますが、ビデオ ファイルをダウンロードした後、指定したフォルダーにコピーされ、サイズが 10 バイトと示され、再生できません。 (破損しているように見えます)。誰かがこの問題を克服する方法を私に提案できますか? 私が使用したコードはここに添付されています。
** FTP サーバーに接続する**
public FTPClient mFTPClient = null;
public boolean ftpConnect(String host, String username,String password, int port)
{
try {
mFTPClient = new FTPClient();
mFTPClient.connect(host,port);
// now check the reply code, if positive mean connection success
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode()))
{
// login using username & password
boolean status = mFTPClient.login(username, password);
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
return status;
}
}
catch(Exception e)
{
System.out.println("Error: could not connect to host " + host);
e.printStackTrace();
}
return false;
}
ファイルのダウンロード
public boolean ftpDownload(String srcFilePath, String desFilePath)
{
boolean status = false;
try {
FileOutputStream desFileStream = new FileOutputStream(desFilePath);
status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
desFileStream.close();
return status;
} catch (Exception e) {
System.out.println( "download failed");
e.printStackTrace();
}
return status;
}