I wrote a code for compressing database file (.zip) and uploading to an FTP server. When I download that from server, files are corrupted. What can be the reason for that?
Code:
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();
}
A file upload initiated by 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.