我使用以下代码段将文件下载到Android手机的SD卡中。在Wireshark跟踪中,我看到文件已传输,FTP传输完成消息也是如此。但SD卡中的文件大小为零。所有必需的权限都在清单文件中设置。有人可以帮忙吗?不使用FileOutputStream。
try{
ftpClient.connect(InetAddress.getByName("xxx.xxx.xxx.xxx"));
ftpClient.login("xxxx", "xxxxx");
ftpClient.changeWorkingDirectory("xxxx");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
//FileOutputStream desFileStream = new FileOutputStream("/sdcard/test25.txt");
BufferedOutputStream desFileStream = new BufferedOutputStream(new FileOutputStream("/sdcard/test25.txt"),8*1024);
boolean status=ftpClient.retrieveFile("test.txt", desFileStream);
if(status){
Toast toast = Toast.makeText(getApplicationContext(), "Downlaoded", Toast.LENGTH_SHORT);
toast.show();
}
//desFileStream.flush();
ftpClient.logout();
ftpClient.disconnect();
}
catch (IOException e){
Toast toast = Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT);
toast.show();
}
答案 0 :(得分:2)
答案 1 :(得分:1)
完成写入后,您需要关闭输出流:
BufferedOutputStream desFileStream = new BufferedOutputStream(new FileOutputStream("/sdcard/test25.txt"),8*1024);
boolean status=ftpClient.retrieveFile("test.txt", desFileStream);
desFileStream.close();