使用apache commons将多个文件FTP到本地目录 [英] FTP multiple files using apache commons into a local directory

查看:161
本文介绍了使用apache commons将多个文件FTP到本地目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用apache commons将目录中的所有文件下载到我的本地计算机上:

I am trying to download All files in a directory to my local machine using apache commons like this:

import java.io.FileOutputStream;
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPFile;

public class FTPExample {
    public static void main(String[] args) throws SocketException, IOException {
        FTPClient client = new FTPClient();
        client.connect("MyHostName");
        client.enterLocalPassiveMode();
        client.login("username", "password");
        FTPFile[] files = client.listFiles("/App/");
        for (FTPFile file : files) {
            System.out.println(file.getName());
          FileOutputStream fos = new FileOutputStream("Ftp Files/"  + file.getName());
            client.retrieveFile(file.getName(),fos);             
        }
    }
}

能够列出目录中的文件,但是尝试下载文件时出现FilenotFound异常.请帮忙. 我的错误是:

Am able to list the Files in the Directory but I am Getting FilenotFound Exception when trying to Download the files. Please help. My Error is:

Exception in thread "main" java.io.FileNotFoundException: Ftp Files\01 (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
    at ftpexample.FTPExample.main(FTPExample.java:30)
Java Result: 1

我需要将文件以其原始文件名存储在Folder Ftp File/中.

推荐答案

感谢那些尝试提供帮助的人.我在这里找到了解决我问题的答案.这是我的方法:

Thank you to those who tried to help. I found the answer to my problem here. this is How I did it:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


public class FTPExample {

    public static void main(String[] args) {
        try {

            //new ftp client
            FTPClient ftp = new FTPClient();
            //try to connect
            ftp.connect("MyHhostName");
            //login to server
            if (!ftp.login("username", "password")) {
                ftp.logout();
            }
            int reply = ftp.getReplyCode();
            //FTPReply stores a set of constants for FTP reply codes. 
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
            }

            //enter passive mode
            ftp.enterLocalPassiveMode();
            //get system name
            System.out.println("Remote system is " + ftp.getSystemType());
            //change current directory
            ftp.changeWorkingDirectory("/App/PMIGENV/BACK/Finacle/FC/app/CDCI_LOGS/log/UBSADMIN");
            System.out.println("Current directory is " + ftp.printWorkingDirectory());

            //get list of filenames
            FTPFile[] ftpFiles = ftp.listFiles();

            if (ftpFiles != null && ftpFiles.length > 0) {
                //loop thru files
                for (FTPFile file : ftpFiles) {
                    if (!file.isFile()) {
                        continue;
                    }
                    System.out.println("File is " + file.getName());
                    //get output stream
                    OutputStream output;
                    output = new FileOutputStream("FtpFiles" + "/" + file.getName());
                    //get the file from the remote system
                    ftp.retrieveFile(file.getName(), output);
                    //close output stream
                    output.close();

                    //delete the file
                    // ftp.deleteFile(file.getName());

                }
            }

            ftp.logout();
            ftp.disconnect();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

这篇关于使用apache commons将多个文件FTP到本地目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆