Загрузка файла размером 0 байт с помощью Apache Commons FTPSClient

При использовании Apache Commons FTPSClient загруженный файл всегда сохраняется как 0 байт. Я пробовал разные методы, но всегда получаю 0 байтов в сохраненном файле. В коде ниже я показываю все три метода.

Вот используемый код:

public static void main(String[] args) {
String server = "ftps-url";
int port = 6321;
String user = "";
String pass = "";

FTPSClient ftp = null;
try {
    ftp = new FTPSClient("SSL");
    ftp.setAuthValue("SSL");
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

    int reply;

    ftp.connect(server, port);
    System.out.println("Connecting");
    System.out.print(ftp.getReplyString());

    // After connection attempt, you should check the reply code to verify success.
    reply = ftp.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        System.err.println("FTP server refused connection.");
        System.exit(1);
    }
    ftp.login(user, pass);

    ftp.execPBSZ(0);
    ftp.execPROT("P");

    // ... // transfer files
    ftp.setBufferSize(1000);
    ftp.enterLocalPassiveMode();
    // ftp.setControlEncoding("GB2312");
    ftp.changeWorkingDirectory("/output"); //path where my files are
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    //System.out.println("Remote system is " + ftp.getSystemName());

    String[] filelist = ftp.listNames();  //returns null
    System.out.println(filelist.length);
    System.out.println(Arrays.toString(filelist));

    // method 1
    File inFile = new File("myfile.xls");
    if (!inFile.exists()) {               
        inFile.createNewFile();
    }
    InputStream input = new FileInputStream(inFile);
    ftp.completePendingCommand();
    ftp.storeFile(filelist[0], input);
    input.close();

    // method 2
    FileOutputStream fos = new FileOutputStream("myfile.xls");
    ftp.retrieveFile(filelist[0], fos);
    fos.flush();
    fos.close();

    //ftp.completePendingCommand();

    // method 3
    File path = new File("C:/Users/user/Desktop/");
    String remoteFilePath = "/output/" + filelist[0];
    File resultFile = new File(path, filelist[0]);
    CheckedOutputStream fout = new CheckedOutputStream(new FileOutputStream(resultFile), new CRC32());
    ftp.retrieveFile(remoteFilePath, fout);
    } catch (Exception ex) {
        System.out.println("error");
        ex.printStackTrace();
    } finally {
        // logs out and disconnects from server
        try {
            if (ftp.isConnected()) {
                ftp.logout();
                ftp.disconnect();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

person csaffi    schedule 27.01.2016    source источник


Ответы (1)


Метод 1 представляет собой загрузку, а не загрузку, и он затирает файл на сервере файлом нулевой длины, поэтому методы 2 и 3 правильно извлекают файл нулевой длины.

person user207421    schedule 27.01.2016
comment
Спасибо за ваш ответ @EJP, вы правы, проблема заключалась в том, что вызов storeFile создал на сервере файл с нулевым байтом. Какая тривиальная ошибка с моей стороны! :) - person csaffi; 28.01.2016