java使用ftp连接linux处理文件

发布时间 2023-12-07 16:53:22作者: sowler

1、Maven依赖

     <!-- FTP使用包 -->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
        </dependency>

2、使用方式

package com.task;

import org.apache.commons.net.ftp.*;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class RecordTask {
    public static void main(String[] args) throws Exception {
        // Linux服务器IP或主机名
        String server = "your_server";
        // FTP端口号(默认为21)
        int port = Integer.valueOf("your_port");
        // FTP登录用户名
        String userName = "your_username";
        // FTP登录密码
        String password = "your_password";

        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(userName, password);
            // 设置被动模式传输数据
            ftpClient.enterLocalPassiveMode();
            // 切换到指定目录
            ftpClient.changeWorkingDirectory("/opt/test");
            // 列出当前目录下的所有文件
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file : files) {
                if (!file.isDirectory()) {
                    // 将文件从Linux服务器下载到本地
                    File localFile = new File("local_directory/" + file.getName());
                    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
                    InputStream inputStream = ftpClient.retrieveFileStream(file.getName());
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                    }
                    outputStream.close();
                    inputStream.close();
                }
            }
            System.out.println("文件下载完成!");
        } finally {
            if(ftpClient!=null && ftpClient.isConnected()){
                try {
                    //退出登录
                    ftpClient.logout();
                    //断开连接
                    ftpClient.disconnect();
                    System.out.println("退出登录,断开连接");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除文件
     */
    public void deleteFile(FTPClient ftpClient,String fileName){
        if(ftpClient == null){
            return;
        }
        //String fileName = "/opt/test/测试.txt";
        String validFileName = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
        boolean flag ;
        try {
            flag = ftpClient.deleteFile(validFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(ftpClient!=null && ftpClient.isConnected()){
                try {
                    //退出登录
                    ftpClient.logout();
                    //断开连接
                    ftpClient.disconnect();
                    System.out.println("退出登录,断开连接");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}