Reputation: 7228
I'm trying to transfer a files(config.zip.zip and artikels.csv) from my Java application to an FTP server the program works, the file is transferred, but when I go to open it in the folder, the file is corrupted, file size is less then original file size. artikels.csv file has less row then orignal artikels.csv but the zip file can't be open totally. Why? And how can I fix this?
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public static void upload(List<String> files){
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(ftpServer);
client.login(username, password);
client.setFileType(FTP.BINARY_FILE_TYPE);
for(String filename:files){
fis = new FileInputStream(filename);
boolean uploadOk = client.storeFile(FilenameUtils.getName(filename), fis);
System.out.println("Uploading status is: "+uploadOk);
}
client.completePendingCommand();
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Views: 2680
Reputation:
You need to add fis.close()
inside your loop.
Since you are looping through many files you need to close your FileInputStream
after each one.
Upvotes: 0
Reputation: 1095
Is this the exact code that you are running? It seems suspect that the completePending Command() and logout() are inside your file iteration loop. Shouldn't you have something more like the following?
...
client.connect(ftpServer);
client.login(username, password);
client.setFileType(FTP.BINARY_FILE_TYPE);
for(String filename:files){
fis = new FileInputStream(filename);
String s = FilenameUtils.getName(filename);
System.out.println(s);
boolean uploadOk = client.storeFile(s, fis);
System.out.println("Uploading status is: "+uploadOk);
}
client.completePendingCommand();
client.logout();
...
Upvotes: 1