使用hutool进行ftp文件下载和上传

2 篇文章 0 订阅

1 引入依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.15</version>
</dependency>

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.6</version>
</dependency>

2 工具类

package ftp;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ftp.Ftp;
import cn.hutool.extra.ftp.FtpMode;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;

/**
 * @author : 
 * @description :
 * @date : 2023/3/10 14:42
 */
public class FtpUtil {
    /**
     * 下载ftp服务器上的文件到本地
     * @param remoteFile
     * @param localFile
     * @param ip
     * @param port
     * @param username
     * @param password
     * @param ftpMode
     * @return 成功则返回字符串:success
     */
    public static String download(String remoteFile, String localFile, String ip, Integer port, String username, String password, FtpMode ftpMode) {
        if(StringUtils.isBlank(localFile)) {
            return "本地保存路径及名称不能为空";
        }
        File lFile = FileUtil.file(localFile);
        Ftp ftp = null;
        try {
            //匿名登录(无需帐号密码的FTP服务器)
            ftp = new Ftp(ip,port == null ? 21 : port,username,password);
            if(ftpMode != null) {
                ftp.setMode(ftpMode);
            }
            ftp.download(remoteFile, lFile);
        } catch (Exception e) {
            return e.getMessage();
        } finally {
            //关闭连接
            try {
                if(ftp != null)  ftp.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if(lFile.exists() && lFile.length() > 0) {
            return "success";
        } else {
            lFile.delete();
            return "download failure,"+ remoteFile +" maybe not exists !!";
        }
    }

    /**
     * 此方法不指定上传后保存的名称, 则按本地文件的名称保存
     * @param remoteDir
     * @param localFile
     * @param ip
     * @param port
     * @param username
     * @param password
     * @return 成功则返回字符串:success
     */
    public static String upload(String remoteDir, String localFile, String ip, Integer port, String username, String password, FtpMode ftpMode) {
        return upload(remoteDir, null, localFile, ip, port, username, password, ftpMode);
    }

    /**
     *
     * @param remoteDir 上传的ftp目录
     * @param remoteFileName  保存到ftp服务器上的名称
     * @param localFile 本地文件全名称
     * @param ip
     * @param port
     * @param username
     * @param password
     * @return 成功则返回字符串:success
     */
    public static String upload(String remoteDir, String remoteFileName, String localFile, String ip, Integer port, String username, String password, FtpMode ftpMode) {
        if(StringUtils.isBlank(localFile)) {
            return "本地文件名称不能为空";
        }
        File lFile = FileUtil.file(localFile);
        if(!lFile.exists()) {
            return "本地文件不存在";
        }
        Ftp ftp = null;
        try {
            //匿名登录(无需帐号密码的FTP服务器)
            ftp = new Ftp(ip,port == null ? 21 : port,username,password);
            if(ftpMode != null) {
                ftp.setMode(ftpMode);
            }
            if(StringUtils.isBlank(remoteFileName)) {
                ftp.upload(remoteDir, lFile);
            } else {
                ftp.upload(remoteDir, remoteFileName, lFile);
            }
        } catch (Exception e) {
            return e.getMessage();
        } finally {
            //关闭连接
            try {
                if(ftp != null)  ftp.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return "success";
    }
}

3 测试

public class FtpTest {

    public static void main(String[] args) {
        //上传文件到ftp
        String result = FtpUtil.upload("opt/upload","fff.zip", "D:/STM.zip", "192.168.68.55", 21, "ftpuser", "ftpuser!@#123", null);
        System.out.println(result);

        //下载远程文件
        String result2 = FtpUtil.download("opt/upload/fff.zip", "D:/bbb.zip", "192.168.68.55", 21, "ftpuser", "ftpuser!@#123", null);
        System.out.println(result2);

    }
}
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值