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 {
            // 断开连接代码省略
        }
    }
}
相关推荐
砍材农夫1 小时前
spring|spring web|拦截器、过滤器、aop
java·spring boot·spring·spring cloud
考虑考虑8 小时前
docker compose V2版本新属性
运维·后端·自动化运维
weixin_BYSJ198710 小时前
【计算机毕设】基于SpringBoot与Vue的文物保护档案管理系统08621
vue.js·spring boot·spring cloud·微服务·架构·django·课程设计
GreenTea10 小时前
7000 万 QPS、500 PB:OpenAI 如何用一个 Python 存储平台撑住 10 亿用户
后端·架构
Flynt10 小时前
Java 27 升级实测:默认值动得比新特性多,有个老参数会让 JVM 直接起不来
java·jvm·后端
Bs_MoneyMagnet10 小时前
基于springboot+vue的个人健康管理系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·vue3·springboot3·计算机毕业设计
GreenTea10 小时前
OpenAI Agents API 上手实测:一次调用把整个 agent loop 甩给 OpenAI
前端·后端·算法
Bs_MoneyMagnet13 小时前
基于springboot+vue的心理咨询预约与随访平台的设计与实现 源码+文档
vue.js·spring boot·后端·spring·毕业设计·旅游·计算机毕业设计
IT_陈寒13 小时前
Vue的响应式更新把我坑惨了,原来问题出在这
前端·人工智能·后端
陌シ未央ゞ14 小时前
基于BM25算法和RRF实现的混合索引(java版)
人工智能·spring boot·后端·算法