坑1:
下载后需要使用,需要3步,1将jar文件放到libs文件夹内,2将ftp4j-1.7.2带源码的整个文件夹放到libs内,3app-gradle中增加
dependencies{
implementation files('libs/ftp4j-1.7.2.jar')
}
坑2:
从ftp下载文件失败,据说ftpClient.setPassive(true);要放到connect和login中间,否则下载文件大小为0.我的代码如下:
public static boolean DowLoadFile(String server_path, String local_path, String file_name)
{
OutputStream outputStream =null;
try {
String local_file_name = local_path + file_name;
FTPClient ftpClient =new FTPClient();
ftpClient.connect("***.***.***.***", 1234);
ftpClient.setPassive(true);
ftpClient.setType(FTPClient.TYPE_BINARY);
ftpClient.login("name", "pwd");
ftpClient.changeDirectory(server_path);
ftpClient.download(file_name, new File(local_file_name));
String content = FileUtil.getFileContents(local_file_name);
Log.e(TAG, "content: " + content);
ftpClient.disconnect(true);
}catch (FTPIllegalReplyException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}catch (FTPException | FTPDataTransferException | FTPAbortedException e) {
e.printStackTrace();
}
return true;
}
坑3:
开始大小为0,后来代码改对了,但是通过电脑同步看到的仍然是0。绝望中偶尔读了一下文件,发现能读出内容来。唉,不靠谱的android。
网友评论