小编给大家分享一下SpringBoot项目如何集成FTP,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!FTP相关软件安装我在此就不介绍如何安装FTP了,但是我可以推荐给大家一些软件作为选择。linux版本,推荐使用vsft
小编给大家分享一下SpringBoot项目如何集成FTP,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
我在此就不介绍如何安装FTP了,但是我可以推荐给大家一些软件作为选择。
linux版本,推荐使用vsftpd进行搭建FTP,只需要改指定的几个配置,添加上用户即可。
windows版本,推荐使用Serv-U进行搭建FTP,图形化界面,有中文版,操作起来很简单。
这里我们对FTP相关的组件包使用的是edtFTPj,其实之前很多人都选择的是Java自带的包来实现FTP功能的。
在我们的springBoot项目中pom.xml下添加以下依赖。
<dependency> <groupId>com.enterprisedt</groupId> <artifactId>edtFTPj</artifactId> <version>1.5.3</version></dependency>
更新Maven进行引入,然后我们进行下一步。
引入两个工具类。
我这里先贡献一下代码,请大家酌情作为参考。
public class FtpHelper { private FTPClient ftp; public FtpHelper() { } public FtpHelper(String ftpServer, int ftpPort, String ftpUsername, String ftpPassWord) { connect(ftpServer, ftpPort, ftpUsername, ftpPassword); } 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; } } public boolean checkDirectory(FTPClient ftp, String dirName) { boolean flag; try { ftp.chdir(dirName); flag = true; } catch (Exception e) { e.printStackTrace(); flag = false; } return flag; } public void disconnect() { try { if (ftp.connected()) { ftp.quit(); } } catch (Exception e) { e.printStackTrace(); } } 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; } public void uploadFile(File file, String filePath) throws Exception { InputStream inStream = new FileInputStream(file); uploadFile(inStream, filePath); } 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); } 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); } public void changeDirectory(String path) { if (!ValidateUtils.isEmpty(path)) { try { ftp.chdir(path); } catch (Exception e) { e.printStackTrace(); } } }}
public class FtpHelper { private FTPClient ftp; public FtpHelper() { } public FtpHelper(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { connect(ftpServer, ftpPort, ftpUsername, ftpPassword); } 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; } } public boolean checkDirectory(FTPClient ftp, String dirName) { boolean flag; try { ftp.chdir(dirName); flag = true; } catch (Exception e) { e.printStackTrace(); flag = false; } return flag; } public void disconnect() { try { if (ftp.connected()) { ftp.quit(); } } catch (Exception e) { e.printStackTrace(); } } 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; } public void uploadFile(File file, String filePath) throws Exception { InputStream inStream = new FileInputStream(file); uploadFile(inStream, filePath); } 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); } 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); } 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”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网精选频道,感谢各位的阅读!
--结束END--
本文标题: SpringBoot项目如何集成FTP
本文链接: https://www.lsjlt.com/news/304244.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0