iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >使用hutool进行ftp文件下载和上传
  • 523
分享到

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

java 2023-10-20 14:10:37 523人浏览 薄情痞子
摘要

1 引入依赖 cn.hutool hutool-all 5.8.15 commons-net commons-net 3.6 2 工具类 package ftp;

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;public class FtpUtil {        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 !!";        }    }        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);    }        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);    }}

来源地址:https://blog.csdn.net/qq_41273205/article/details/129443445

--结束END--

本文标题: 使用hutool进行ftp文件下载和上传

本文链接: https://www.lsjlt.com/news/433029.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作