简体   繁体   English

使用Apache Commons FTPSClient下载0字节文件

[英]0 bytes file download with Apache Commons FTPSClient

Using Apache Commons FTPSClient the downloaded file is always stored as 0 bytes. 使用Apache Commons FTPSClient,下载的文件始终存储为0字节。 I tried different methods, but get always 0 bytes on the stored file. 我尝试了不同的方法,但在存储的文件上始终获得0字节。 On the code below I show all three methods. 在下面的代码中,我展示了所有三种方法。

Here is the used code: 这是使用的代码:

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();
        }
    }
}

方法1是上载,而不是下载,它使用零长度文件在服务器上掩盖了文件,因此方法2和3都正确地检索了零长度文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM