| package com.ruoyi.project.utils; |
| |
| import java.io.*; |
| |
| import ch.qos.logback.classic.Logger; |
| import com.ruoyi.project.tool.tcc.LogbackUtil; |
| import org.apache.commons.net.ftp.*; |
| |
| public class FtpUtil { |
| private Logger logger = LogbackUtil.getLogger("FtpUtil"); |
| private FTPClient ftpClient; |
| |
| public static void main(String[] args) throws Exception { |
| FtpUtil ftpUtil = new FtpUtil(); |
| FtpModel ftpModel = ftpUtil.getFtpModel("192.168.1.4", 21, "admin", "******"); |
| String connect = ftpUtil.connect(ftpModel); |
| System.out.println("======== connect " + connect); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| String deleteFile = ftpUtil.deleteFile("./testDir", "FtpUtil.java"); |
| System.out.println("======== deleteFile " + deleteFile); |
| } |
| |
| public String connect(FtpModel ftpModel) { |
| ftpClient = new FTPClient(); |
| String res; |
| try { |
| ftpClient.connect(ftpModel.getIp(), ftpModel.getPort()); |
| ftpClient.login(ftpModel.getUserName(), ftpModel.getPassword()); |
| ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); |
| ftpClient.setControlEncoding("UTF-8"); |
| |
| ftpClient.enterLocalPassiveMode(); |
| int reply = ftpClient.getReplyCode(); |
| if (!FTPReply.isPositiveCompletion(reply)) { |
| ftpClient.disconnect(); |
| res = "-1 " + reply; |
| } else { |
| res = "0 " + reply; |
| } |
| } catch (Exception e) { |
| logger.error("连接ftp服务失败" + e.getMessage(), e); |
| e.printStackTrace(); |
| res = "-1 " + e.getMessage(); |
| } |
| return res; |
| } |
| |
| public String uploadFile(String remoteBaseDir, String fileName) throws Exception { |
| try { |
| boolean makeDirectory = ftpClient.makeDirectory(remoteBaseDir); |
| boolean b1 = ftpClient.changeWorkingDirectory(remoteBaseDir); |
| File file = new File(fileName); |
| FileInputStream fileInputStream = new FileInputStream(file); |
| boolean b = ftpClient.storeFile(file.getName(), fileInputStream); |
| fileInputStream.close(); |
| return b ? "0" : "-1"; |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return "-1 " + e.getMessage(); |
| } |
| } |
| |
| public String downloadFile(String remoteFileName, String saveDir, String fileName) { |
| String res; |
| try { |
| File fileDir = new File(saveDir); |
| if (!fileDir.exists()) { |
| fileDir.mkdirs(); |
| } |
| File localFile = new File(saveDir + File.separatorChar + fileName); |
| OutputStream os = new FileOutputStream(localFile); |
| |
| boolean flag2 = ftpClient.retrieveFile(new String(remoteFileName.getBytes("GBK"), "iso-8859-1"), os); |
| os.close(); |
| closeFtp(); |
| if (!flag2) { |
| localFile.delete(); |
| res = "-1 " + "没有找到 " + remoteFileName + " 该文件"; |
| } else { |
| res = "0"; |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| res = "-1 " + e.getMessage(); |
| } |
| return res; |
| } |
| |
| public void closeFtp() { |
| if (ftpClient != null && ftpClient.isConnected()) { |
| try { |
| ftpClient.logout(); |
| ftpClient.disconnect(); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| |
| |
| |
| public void upload(File file) throws Exception { |
| if (!file.isDirectory()) { |
| FileInputStream fileInputStream = new FileInputStream(file); |
| ftpClient.storeFile(file.getName(), fileInputStream); |
| fileInputStream.close(); |
| } else { |
| ftpClient.makeDirectory(file.getName()); |
| ftpClient.changeWorkingDirectory(file.getName()); |
| String[] files = file.list(); |
| for (String fileStr : files) { |
| File file1 = new File(file.getPath() + "/" + fileStr); |
| if (file1.isDirectory()) { |
| upload(file1); |
| ftpClient.changeToParentDirectory(); |
| } else { |
| File file2 = new File(file.getPath() + "/" + fileStr); |
| FileInputStream fileInputStream = new FileInputStream(file2); |
| ftpClient.storeFile(file2.getName(), fileInputStream); |
| fileInputStream.close(); |
| } |
| } |
| } |
| } |
| |
| |
| public String downloadDirFiles(FtpModel ftpModel, String localBaseDir, String remoteBaseDir) { |
| String connect = connect(ftpModel); |
| if (connect.startsWith("-1")) { |
| return "-1 连接失败"; |
| } |
| try { |
| FTPFile[] files; |
| boolean isChangeDir = ftpClient.changeWorkingDirectory(remoteBaseDir); |
| if (!isChangeDir) { |
| return "-1 changeWorkingDirectory fail"; |
| } |
| ftpClient.setControlEncoding("UTF-8"); |
| files = ftpClient.listFiles(); |
| for (FTPFile ftpFile : files) { |
| downloadFile(ftpFile, localBaseDir); |
| |
| } |
| closeFtp(); |
| return "0"; |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return "-1 下载过程中出现异常" + e.getMessage(); |
| } |
| } |
| |
| private void downloadFile(FTPFile ftpFile, String localBaseDir) { |
| OutputStream outputStream; |
| try { |
| String localFilePath = localBaseDir + File.separatorChar + ftpFile.getName(); |
| File locaFile = new File(localFilePath); |
| if (locaFile.exists()) { |
| return; |
| } |
| File fileParent = locaFile.getParentFile(); |
| if (!fileParent.exists()) { |
| fileParent.mkdirs(); |
| } |
| outputStream = new FileOutputStream(localFilePath); |
| ftpClient.retrieveFile(ftpFile.getName(), outputStream); |
| outputStream.flush(); |
| outputStream.close(); |
| } catch (Exception e) { |
| logger.error(e.toString()); |
| e.printStackTrace(); |
| } |
| } |
| |
| public String deleteFile(String remoteDir, String remoteFileName) { |
| String res; |
| try { |
| ftpClient.changeWorkingDirectory(remoteDir); |
| int dele = ftpClient.dele(remoteFileName); |
| |
| res = dele == 250 ? "0" : "-1"; |
| } catch (Exception e) { |
| logger.error("删除的文件失败"); |
| e.printStackTrace(); |
| res = "-1 " + e.getMessage(); |
| } |
| return res; |
| } |
| |
| public FtpModel getFtpModel(String ip, Integer port, String userName, String password) { |
| return new FtpModel(ip, port, userName, password); |
| } |
| |
| public class FtpModel { |
| public String ip; |
| public Integer port; |
| public String userName; |
| public String password; |
| |
| public String getIp() { |
| return ip; |
| } |
| |
| public void setIp(String ip) { |
| this.ip = ip; |
| } |
| |
| public Integer getPort() { |
| return port; |
| } |
| |
| public void setPort(Integer port) { |
| this.port = port; |
| } |
| |
| public String getUserName() { |
| return userName; |
| } |
| |
| public void setUserName(String userName) { |
| this.userName = userName; |
| } |
| |
| public String getPassword() { |
| return password; |
| } |
| |
| public void setPassword(String password) { |
| this.password = password; |
| } |
| |
| public FtpModel() { |
| } |
| |
| public FtpModel(String ip, Integer port, String userName, String password) { |
| this.ip = ip; |
| this.port = port; |
| this.userName = userName; |
| this.password = password; |
| } |
| } |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律