Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache Commons FTPSClient下载0字节文件_Java_Ftp_Ftps_Apache Commons Net - Fatal编程技术网

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

Java 使用Apache Commons FTPSClient下载0字节文件,java,ftp,ftps,apache-commons-net,Java,Ftp,Ftps,Apache Commons Net,使用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 {

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

方法1是一个上传,而不是下载,它在服务器上用一个零长度的文件重击文件,因此方法2和方法3都正确地检索到一个零长度的文件。

感谢您的回答@EJP,您是对的,问题是调用storeFile在服务器上创建了一个零字节的文件。我犯了一个多么微不足道的错误啊!:)