Ana*_*nth 3 android ftp-client apache-commons-net
我是Android的新手.我正在尝试使用Apache Commons FTPClient将文件从ftp服务器下载到SD卡.行 InputStream input = client.retrieveFileStream("/"+ fileName); 始终返回null.但该文件位于Ftp位置.请帮助我知道错误在哪里.
我在清单中设置了以下权限; android:name ="android.permission.INTERNET"和android:name ="android.permission.WRITE_EXTERNAL_STORAGE"
我的守则
private static void downLoad(){
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect("ftp.doamin.com");
client.login("8888", "8888");
String filePath = "/mnt/sdcard/download/CheckboxHTML.txt" ;
String fileName = "CheckboxHTML.txt";
fos = new FileOutputStream(filePath);
InputStream input = client.retrieveFileStream("/" + fileName);
byte[] data = new byte[1024];
int count = input.read(data);
while ((count = input.read(data)) != -1) {
fos.write(data, 0, count);
}
fos.close();
if(!client.completePendingCommand()) {
client.logout();
client.disconnect();
System.err.println("File transfer failed.");
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您的时间和兴趣.Ananth.
据我所知.您可以通过调用completePendingCommand()并验证传输确实成功来完成文件传输.即你需要在fos.clos()下面添加调用函数.
fos.close();
client.completePendingCommand()
Run Code Online (Sandbox Code Playgroud)
您也可以考虑这一点,根据FTPClient.retrieveFileStream()的API,该方法在无法打开数据连接时返回null,在这种情况下您应该检查回复代码(例如getReplyCode(),getReplyString(),getReplyStrings( ))看看它失败的原因.
| 归档时间: |
|
| 查看次数: |
9597 次 |
| 最近记录: |