springboot整合FTP实现文件传输

实现ftp文件传输的步骤:

1.ftp绑定ip端口登录

2.切换到指定地址

3.文件下载

4.关闭ftp连接

项目中使用的jar包

java 复制代码
  <!--        ftp包-->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.9.0</version>
        </dependency>

项目中使用ftp代码:

java 复制代码
  public void getQxjFile() throws IOException {
        FTPClient ftpClient = new FTPClient(); //创建FTP连接客户端
        ftpClient.enterLocalPassiveMode();// 设置被动模式
        //ftp设置ip,端口
        ftpClient.connect(costomDefineData.getQxjIp(), Integer.parseInt(costomDefineData.getQxjPort()));
        //设置调用为被动模式
        ftpClient.enterLocalPassiveMode();
        //ftpClient.enterLocalActiveMode(); 设置为主动模式 
        //设置文件以二进制文件模式传输
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        //ftp登录
        boolean loggedIn = ftpClient.login(costomDefineData.getQxjUserName(), costomDefineData.getQxjPassword());
        if (loggedIn) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
        //切换到登录后的文件夹 这里指定ftp服务器文件存放位置
        boolean changed = ftpClient.changeWorkingDirectory("/");
        if (changed) {
            //获取到对应的FTP文件 这是获取对应文件夹下全部文件
            FTPFile[] files = ftpClient.listFiles();
            System.out.println("获取文件个数" + files.length);
            for (FTPFile file : files) {
                if (file.isFile()) {

                    File localDir = new File(costomDefineData.getQxjFilePath() + YM + "/" + Day);
                    if (!localDir.exists()) {
                        localDir.mkdirs();
                    }
                    File localFile = new File(costomDefineData.getQxjFilePath() + YM + "/" + Day + "/" + file.getName());
                    if (!localFile.exists()) {
                        localFile.createNewFile();
                    }
                    //将ftp服务器上文件同步到本地
                    ftpClient.retrieveFile("/" + file.getName(), new FileOutputStream(localFile));

                    BufferedReader reader = new BufferedReader(new FileReader(localFile));
                    // 读取文件内容并解析
                    String line;
                    String result = "";
                    while ((line = reader.readLine()) != null) {
                        // 解析每一行的数据
                        result = result + line;
                    }
                    }
                    }
                    //实现ftp上文件删除
                     boolean deleted = ftpClient.deleteFile("/" + file.getName());
                    }
                    //ftp用户登出
        ftpClient.logout();
        //ftp去掉链接
        ftpClient.disconnect(); 
        }

使用ftp实现上传功能

java 复制代码
public class FTPExample {
    public static void main(String[] args) {
        FTPClient ftpClient = new FTPClient();
        // 连接和登录代码省略
        
        try {
            // 上传文件
            File localFile = new File("local-file.txt");
            String remoteFile = "remote-file.txt";
            
            FileInputStream inputStream = new FileInputStream(localFile);
            
            boolean uploaded = ftpClient.storeFile(remoteFile, inputStream);
            
            inputStream.close();
            
            if (uploaded) {
                System.out.println("文件上传成功!");
            } else {
                System.out.println("文件上传失败!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 断开连接代码省略
        }
    }
}
相关推荐
q***42057 分钟前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
MegatronKing11 分钟前
Reqable 3.0版本云同步的实践过程
前端·后端·测试
快手技术16 分钟前
闪耀NeurIPS 2025!快手13篇论文入选,Spotlight 成果跻身前三!
前端·后端
Starduster17 分钟前
一次数据库权限小改动,如何拖垮半个互联网?——Cloudflare 2025-11-18 大故障复盘
数据库·后端·架构
一 乐23 分钟前
宠物猫店管理|宠物店管理|基于Java+vue的宠物猫店管理管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·后端·宠物管理
r***998225 分钟前
在2023idea中如何创建SpringBoot
java·spring boot·后端
一 乐40 分钟前
考公|考务考试|基于SprinBoot+vue的考公在线考试系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·课程设计
q***721943 分钟前
Y20030018基于Java+Springboot+mysql+jsp+layui的家政服务系统的设计与实现 源代码 文档
android·前端·后端
i***39581 小时前
ShardingSphere-jdbc 5.5.0 + spring boot 基础配置 - 实战篇
java·spring boot·后端
AY呀1 小时前
DeepSeek:探索AI大模型与开发工具的全景指南
后端·机器学习