FtpUtils,Ftp上下载图片的工具类

本文介绍了一个使用Apache Commons Net库实现的FTPUtils工具类,用于在Android中从FTP服务器下载图片。通过FTPClient进行连接、设置被动模式,然后获取并下载指定路径的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.yuwei.physicalassistantpro.instrument;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.ljy.devring.other.RingLog;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class FtpUtils {
private static final String TAG = FtpUtils.class.getSimpleName();
private FTPClient ftpClient;

public FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) {
    if (ftpClient == null) {
        ftpClient = new FTPClient();
    }
    if (ftpClient.isConnected()) {
        return ftpClient;
    }
    Log.d(TAG, "ftpHost:" + ftpHost + ",ftpPort:" + ftpPort);

    try {
        // connect to the ftp server
        // set timeout
        ftpClient.setConnectTimeout(50000);
        // 设置中文编码集,防止中文乱码
        ftpClient.setControlEncoding("UTF-8");

        ftpClient.connect(ftpHost, ftpPort);
        int replyCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            Log.d(TAG, "connect fail: replyCode:" + replyCode);
            return ftpClient;
        }
        Log.d(TAG, "connect success: replyCode" + replyCode);

    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
        return null;
    }

    Log.d(TAG, "ftpUserName:" + ftpUserName + ",ftpPassword:" + ftpPassword);
    // login on the ftp server
    try {

        ftpClient.login(ftpUserName, ftpPassword);
        int replyCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            ftpClient.disconnect();
            Log.d(TAG, "login fail: replyCode:" + replyCode);
            return ftpClient;
        }

        Log.d(TAG, "login success: replyCode:" + replyCode);
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
        return null;
    }

    return ftpClient;
}


/**
 * 关闭FTP方法
 *
 * @param ftp
 * @return
 */
public boolean closeFTP(FTPClient ftp) {

    try {
        ftp.logout();
    } catch (Exception e) {
        Log.e(TAG, "FTP关闭失败");
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
                Log.e(TAG, "FTP关闭失败");
            }
        }
    }

    return false;

}


/**
 * 下载FTP下指定文件
 *
 * @param ftp      FTPClient对象
 * @param filePath FTP文件路径
 * @param fileName 文件名
 * @param downPath 下载保存的目录
 * @return true:success, false:fail
 */
public boolean downLoadFTP(FTPClient ftp, String filePath, String fileName, String downPath) {
    // 默认失败
    boolean flag = false;

    FTPFile[] files;
    // 跳转到文件目录
    try {

// ftp.changeWorkingDirectory(filePath);
// // 获取目录下文件集合
ftp.enterLocalPassiveMode();
files = ftp.listFiles(filePath);
} catch (IOException e) {
Log.e(TAG, "downLoadFTP: " + e);
e.printStackTrace();
return false;
}

    for (FTPFile file : files) {
        // 取得指定文件并下载
        if (file.getName().equals(fileName)) {
            Log.d(TAG, "fileName:" + fileName);
            File downFile = new File(downPath + file.getName());
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(downFile);
                // 绑定输出流下载文件,需要设置编码集,不然可能出现文件为空的情况
                ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
                flag = ftp.retrieveFile(filePath +"/"+ fileName, fos);
                RingLog.e(flag);
                // 下载成功删除文件,看项目需求
                // ftpClient.deleteFile(new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));
                fos.flush();
                if (flag) {
                    RingLog.d(TAG, "Params downloaded successful.");
                } else {
                    RingLog.e(TAG, "Params downloaded failed.");
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else {
            RingLog.e("文件不一致");
        }
    }
    return flag;
}

/**
 * FTP文件上传工具类
 *
 * @param ftp      ftpClient
 * @param filePath filePath
 * @param ftpPath  ftpPath
 * @return true:success, false:fail
 */
public boolean uploadFile(FTPClient ftp, String filePath, String ftpPath) {
    boolean flag = false;
    InputStream in = null;
    try {
        // 设置PassiveMode传输
        ftp.enterLocalPassiveMode();
        //设置二进制传输,使用BINARY_FILE_TYPE,ASC容易造成文件损坏
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        //判断FPT目标文件夹时候存在不存在则创建
        if (!ftp.changeWorkingDirectory(ftpPath)) {
            ftp.makeDirectory(ftpPath);
        }
        //跳转目标目录
        ftp.changeWorkingDirectory(ftpPath);

        //上传文件
        File file = new File(filePath);
        in = new FileInputStream(file);
        String tempName = ftpPath + File.separator + file.getName();
        flag = ftp.storeFile(new String(tempName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1), in);
        if (flag) {
            Log.d(TAG, "上传成功");
        } else {
            Log.e(TAG, "上传失败");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "上传失败");
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return flag;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kstar_Ming_闫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值