android端集成FTP文件下载功能

我使用的是ftp4j的SDK,非常轻巧易用,可以轻松的实现类似文件管理器的功能,但我的项目需求只要求下载,所以就只实现了下载功能。

官方地址:http://www.sauronsoftware.it/projects/ftp4j/manual.php

文档非常简明易懂:

直接贴上我的代码:

public class FTPUtils {

    public interface Callback {
        void onSuccess(String filePath);

        void onFail();
    }

    /**
     * 下载FTP服务器上面的文件
     * @param url       ftp服务器地址
     * @param userName  登录账号
     * @param password  密码
     * @param callback 下载结果回调
     */
    @SuppressLint("CheckResult")
    public static void download(final String url,
                                final String userName,
                                final String password,
                                final Callback callback) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> subscriber) {
                String downloadLocalPath = "";
                try {
                    String net = url.replace("ftp://", "");
                    String hostAndPort = net.substring(0, net.indexOf("/"));
                    String[] hps = hostAndPort.split(":");
                    FTPClient client = new FTPClient();
                    client.connect(hps[0], Integer.parseInt(hps[1]));
                    client.login(userName, password);
                    String path = net.substring(net.indexOf("/"), net.lastIndexOf("/"));
                    String[] strings = path.split("/");

                    StringBuilder sb = new StringBuilder();
                    for (String directory : strings) {
                        if (TextUtils.isEmpty(directory)) continue;
                        sb.append(directory).append(File.separator);
                    }
                    String directory = sb.toString();
                    //跳转到指定路径
                    client.changeDirectory(directory);
                    String dir = client.currentDirectory();
                    FTPFile[] list = client.list();
                    boolean fileExist = false;
                    String fileName = net.substring(net.lastIndexOf("/") + 1);
                    for (FTPFile file : list) {
                        if (file.getName().equals(fileName)) {
                            fileExist = true;
                            break;
                        }
                    }
                    //找到文件就下载
                    if (fileExist) {
                        downloadLocalPath = FilePathUtils.getIntance(MyApp.getContext())
                                .getDownFilePath() + File.separator + fileName;
                        client.download(fileName, new java.io.File(downloadLocalPath));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                subscriber.onNext(downloadLocalPath);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {

                    @Override
                    public void accept(String s) {
                        if (TextUtils.isEmpty(s)) {
                            callback.onFail();
                        } else {
                            callback.onSuccess(s);
                        }
                    }
                });
    }
}

 看,非常简单,有3点需要注意:

1、client.connect传入的host,不能带ftp://这样的开头,必须直接用172.2.3.9这种格式

2、FTPClient.list()的时候一直报错FTPListParseException,原因跟DOSListParser类的日期格式有关,参考:https://www.cnblogs.com/yhzh/p/5110293.html

解决办法就是不要用jar包,直接修改源代码:

 3、调用changeDirectory方法的时候也报错,记住,不要用"/file/file1/file2"这种路径格式,因为dos系统包括windows只能识别'\'这样的文件夹符号,所以需要用File.separator()

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值