JAVA通过FTP读取服务器文件内容

FTP无用户名、密码使用以下方式传参登录

String username = "anonymous":
String password = null:
@Component
public class FTPExample {
    @Value("${ftp.ip}")
    private String ftpIp;

    @Value("${ftp.port}")
    private Integer ftpPort;

    @Value("${ftp.username}")
    private String username;

    @Value("${ftp.password}")
    private String password;

//    @Scheduled(fixedDelay=120000)
    public void ftp(){
        try {
            FTPClient ftpClient = new FTPClient();
            ftpClient.connect(ftpIp, ftpPort);
            ftpClient.login(username, password);
            ftpClient.enterLocalPassiveMode();
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                System.out.println("连接FTP失败,用户名或密码错误。");
                ftpClient.disconnect();
            } else {
                System.out.println("FTP连接成功!");
                FTPFile[] files = ftpClient.listFiles();
                printFileDetails(files,ftpClient);
            }

            ftpClient.logout();
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void printFileDetails(FTPFile[] files,FTPClient ftpClient) {
        for (FTPFile file : files) {
            String fileName = file.getName();
            try {
                InputStream inputStream = ftpClient.retrieveFileStream(fileName);
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"GBK"));
                String s = null;
                List<String> listStr = new ArrayList<>();
                while ((s = reader.readLine()) != null) {
                    System.out.println("===================>" + s);
                    listStr.add(s);
                }
                inputStream.close();
                reader.close();
                ftpClient.completePendingCommand();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(fileName);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Apache Commons Net库来连接FTP服务器并获取所有文件内容。下面是一个简单的Java代码示例: ```java import java.io.IOException; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; public class FTPExample { public static void main(String[] args) { FTPClient ftp = new FTPClient(); try { ftp.connect("ftp.example.com"); ftp.login("username", "password"); ftp.enterLocalPassiveMode(); ftp.setFileType(FTP.BINARY_FILE_TYPE); FTPFile[] files = ftp.listFiles(); for (FTPFile file : files) { if (!file.isFile()) { continue; } String content = readFileContent(ftp, file.getName()); System.out.println("File: " + file.getName() + ", Content: " + content); } ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } private static String readFileContent(FTPClient ftp, String filename) throws IOException { StringBuilder sb = new StringBuilder(); ftp.retrieveFile(filename, inputStream -> { int c; while ((c = inputStream.read()) != -1) { sb.append((char) c); } }); return sb.toString(); } } ``` 在上面的示例中,我们首先连接到FTP服务器,然后通过`ftp.listFiles()`方法获取所有文件列表。对于每个文件,我们调用`readFileContent()`方法来获取其内容。在`readFileContent()`方法中,我们使用`ftp.retrieveFile()`方法来下载文件读取内容
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值