使用 Apache Common Net FTPClient.storeFileStream 上传的文件已损坏

Files uploaded using Apache Common Net FTPClient.storeFileStream are corrupted

我写了一个压缩数据库文件 (.zip) 并上传到 FTP 服务器的代码。当我从服务器下载它时,文件已损坏。这可能是什么原因?

代码:

FTPClient ftpClient = new FTPClient();      

ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);                 
  
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy.MM.dd");  
LocalDateTime now = LocalDateTime.now(); 
String currentDate = dtf.format(now);
         
String srcFilename = file;

String remoteFile = "STORE_" + currentDate + ".zip";
       
try {
    byte[] buffer = new byte[1024];
    OutputStream fos = ftpClient.storeFileStream(remoteFile);
    try (ZipOutputStream zos = new ZipOutputStream(fos)) {
        File srcFile = new File(srcFilename);
        try (FileInputStream fis = new FileInputStream(srcFile)) {
            zos.putNextEntry(new ZipEntry(srcFile.getName()));
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }
            zos.closeEntry();
        }
    }
}
catch (IOException ioe) {
    System.out.println("Error creating zip file" + ioe);
}

if (ftpClient.isConnected()) {
        ftpClient.logout();
        ftpClient.disconnect();
}

文件上传由FTPClient.storeFileStream method has to be finished by closing the stream and calling FTPClient.completePendingCommand发起:

You must close the OutputStream when you finish writing to it. The OutputStream itself will take care of closing the parent data connection socket upon being closed.

To finalize the file transfer you must call completePendingCommand and check its return value to verify success. If this is not done, subsequent commands may behave unexpectedly.