SpringBoot项目如何集成FTP
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,小编给大家分享一下SpringBoot项目如何集成FTP,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!FTP相关软件安装我在此就不介绍如何安装FTP了,但是我可以推荐给大家一些软件作
千家信息网最后更新 2026年11月02日SpringBoot项目如何集成FTPFTP相关软件安装
开始集成
引入相关jar包
引入FTPUtils.java和FTPHelper.java
如何使用
小编给大家分享一下SpringBoot项目如何集成FTP,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
FTP相关软件安装
我在此就不介绍如何安装FTP了,但是我可以推荐给大家一些软件作为选择。
Linux版本,推荐使用vsftpd进行搭建FTP,只需要改指定的几个配置,添加上用户即可。
Windows版本,推荐使用Serv-U进行搭建FTP,图形化界面,有中文版,操作起来很简单。
开始集成
引入相关jar包
这里我们对FTP相关的组件包使用的是edtFTPj,其实之前很多人都选择的是Java自带的包来实现FTP功能的。
在我们的SpringBoot项目中pom.xml下添加以下依赖。
com.enterprisedt edtFTPj 1.5.3
更新maven进行引入,然后我们进行下一步。
引入FTPUtils.java和FTPHelper.java
引入两个工具类。
我这里先贡献一下代码,请大家酌情作为参考。
/** * Ftp 工具类 */public class FtpHelper { private FTPClient ftp; public FtpHelper() { } /** * 初始化Ftp信息 * * @param ftpServer ftp服务器地址 * @param ftpPort Ftp端口号 * @param ftpUsername ftp 用户名 * @param ftpPassword ftp 密码 */ public FtpHelper(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { connect(ftpServer, ftpPort, ftpUsername, ftpPassword); } /** * 连接到ftp * * @param ftpServer ftp服务器地址 * @param ftpPort Ftp端口号 * @param ftpUsername ftp 用户名 * @param ftpPassword ftp 密码 */ public void connect(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { ftp = new FTPClient(); try { ftp.setControlEncoding("UTF-8"); ftp.setRemoteHost(ftpServer); ftp.setRemotePort(ftpPort); ftp.setTimeout(6000); ftp.setConnectMode(FTPConnectMode.ACTIVE); ftp.connect(); ftp.login(ftpUsername, ftpPassword); ftp.setType(FTPTransferType.BINARY); } catch (Exception e) { e.printStackTrace(); ftp = null; } } /** * 更改ftp路径 * * @param ftp * @param dirName * @return */ public boolean checkDirectory(FTPClient ftp, String dirName) { boolean flag; try { ftp.chdir(dirName); flag = true; } catch (Exception e) { e.printStackTrace(); flag = false; } return flag; } /** * 断开ftp链接 */ public void disconnect() { try { if (ftp.connected()) { ftp.quit(); } } catch (Exception e) { e.printStackTrace(); } } /** * 读取ftp文件流 * * @param filePath ftp文件路径 * @return s * @throws Exception */ public InputStream downloadFile(String filePath) throws Exception { InputStream inputStream = null; String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("没有输入文件路径"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { ftp.chdir(s); } } byte[] data; try { data = ftp.get(fileName); inputStream = new ByteArrayInputStream(data); } catch (Exception e) { e.printStackTrace(); } return inputStream; } /** * 上传文件到ftp * * @param file 文件对象 * @param filePath 上传的路径 * @throws Exception */ public void uploadFile(File file, String filePath) throws Exception { InputStream inStream = new FileInputStream(file); uploadFile(inStream, filePath); } /** * 上传文件到ftp * * @param inStream 上传的文件流 * @param filePath 上传路径 * @throws Exception */ public void uploadFile(InputStream inStream, String filePath) throws Exception { if (inStream == null) { return; } String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("没有输入文件路径"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { if (!checkDirectory(ftp, s)) { ftp.mkdir(s); } } } ftp.put(inStream, fileName); } /** * 删除ftp文件 * * @param filePath 文件路径 * @throws Exception */ public void deleteFile(String filePath) throws Exception { String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("没有输入文件路径"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { if (checkDirectory(ftp, s)) { ftp.chdir(s); } } } ftp.delete(fileName); } /** * 切换目录 * * @param path * @throws Exception */ public void changeDirectory(String path) { if (!ValidateUtils.isEmpty(path)) { try { ftp.chdir(path); } catch (Exception e) { e.printStackTrace(); } } }}/** * Ftp 工具类 */public class FtpHelper { private FTPClient ftp; public FtpHelper() { } /** * 初始化Ftp信息 * * @param ftpServer ftp服务器地址 * @param ftpPort Ftp端口号 * @param ftpUsername ftp 用户名 * @param ftpPassword ftp 密码 */ public FtpHelper(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { connect(ftpServer, ftpPort, ftpUsername, ftpPassword); } /** * 连接到ftp * * @param ftpServer ftp服务器地址 * @param ftpPort Ftp端口号 * @param ftpUsername ftp 用户名 * @param ftpPassword ftp 密码 */ public void connect(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { ftp = new FTPClient(); try { ftp.setControlEncoding("UTF-8"); ftp.setRemoteHost(ftpServer); ftp.setRemotePort(ftpPort); ftp.setTimeout(6000); ftp.setConnectMode(FTPConnectMode.ACTIVE); ftp.connect(); ftp.login(ftpUsername, ftpPassword); ftp.setType(FTPTransferType.BINARY); } catch (Exception e) { e.printStackTrace(); ftp = null; } } /** * 更改ftp路径 * * @param ftp * @param dirName * @return */ public boolean checkDirectory(FTPClient ftp, String dirName) { boolean flag; try { ftp.chdir(dirName); flag = true; } catch (Exception e) { e.printStackTrace(); flag = false; } return flag; } /** * 断开ftp链接 */ public void disconnect() { try { if (ftp.connected()) { ftp.quit(); } } catch (Exception e) { e.printStackTrace(); } } /** * 读取ftp文件流 * * @param filePath ftp文件路径 * @return s * @throws Exception */ public InputStream downloadFile(String filePath) throws Exception { InputStream inputStream = null; String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("没有输入文件路径"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { ftp.chdir(s); } } byte[] data; try { data = ftp.get(fileName); inputStream = new ByteArrayInputStream(data); } catch (Exception e) { e.printStackTrace(); } return inputStream; } /** * 上传文件到ftp * * @param file 文件对象 * @param filePath 上传的路径 * @throws Exception */ public void uploadFile(File file, String filePath) throws Exception { InputStream inStream = new FileInputStream(file); uploadFile(inStream, filePath); } /** * 上传文件到ftp * * @param inStream 上传的文件流 * @param filePath 上传路径 * @throws Exception */ public void uploadFile(InputStream inStream, String filePath) throws Exception { if (inStream == null) { return; } String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("没有输入文件路径"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { if (!checkDirectory(ftp, s)) { ftp.mkdir(s); } } } ftp.put(inStream, fileName); } /** * 删除ftp文件 * * @param filePath 文件路径 * @throws Exception */ public void deleteFile(String filePath) throws Exception { String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("没有输入文件路径"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { if (checkDirectory(ftp, s)) { ftp.chdir(s); } } } ftp.delete(fileName); } /** * 切换目录 * * @param path * @throws Exception */ public void changeDirectory(String path) { if (!ValidateUtils.isEmpty(path)) { try { ftp.chdir(path); } catch (Exception e) { e.printStackTrace(); } } }}如何使用
public static void main(String[] args) { try { // 从ftp下载文件 FtpHelper ftp = new FtpHelper("127.0.0.1", 21, "root", "123456"); File file = new File("D:\1.doc"); ftp.uploadFile(file, "test/weradsfad2.doc"); ftp.disconnect(); } catch (Exception e) { e.printStackTrace(); } }看完了这篇文章,相信你对"SpringBoot项目如何集成FTP"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
文件
路径
输入
用户
口号
地址
密码
服务器
用户名
服务
项目
工具
推荐
信息
对象
版本
目录
篇文章
软件
链接
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网加科技
关于新基建和互联网科技
互联网络科技有限公司
甘肃拓世互联网科技有限公司
现代科技和互联网有关系吗
深圳国际航运科技和互联网
中国科技互联网公司收入
互联网软件和科技
赶超互联网科技 微信多开
移动互联网科技公司
银川新华互联网科技学校联系电话
互联网科技公司营业执照怎么办理
中国互联网有什么科技
35岁以上互联网科技行业
互联网领先科技活动
银川新华互联网科技学校原画
安徽零酒互联网科技有限公司
杭州融税互联网科技有限公司
伍佰万互联网科技是干嘛的
山东亿川互联网科技有限责任公司
互联网科技公司
互联网科技工作委员会副主任毛军
互联网科技公司华为
互联网科技的
互联网科技金融
北京禾田雨橡互联网科技
一五八零互联网科技
西藏七维空间互联网科技有限公司
云南省自由互联网科技有限公司
优雇互联网科技有限公司