I am uploading few zip files from a directory to a server and after successful upload I am deleting all these zip files.
When I comment my Upload code delete works fine, but when I uncomment the upload code, deleting the zip files fail.
I have referred various questions from SO but none of them helped.
Can someone please help me with the issue, below is my code,
UploadCode
FileWriter fw = new FileWriter("log.txt");
ChannelSftp ccha = null;
Session se = null;
try {
JSch s = new JSch();
se = s.getSession("user", "hostname", 22);
se.setPassword("passwd");
se.setConfig("StrictHostKeyChecking", "no");
se.connect();
ccha = (ChannelSftp)se.openChannel("sftp");
ccha.connect();
ccha.cd("dummy");
File folder = new File("folders");
int count = 1;
for(File file : folder.listFiles()) {
try {
ccha.put(new FileInputStream(file), file.getName());
try {
ccha.ls(file.getName());
System.out.println("success");
}
catch(Exception e) {
System.out.println("fail");
fw.append("failed for - " + file.getName() + " -- " + e.getMessage() + "\n");
}
}
catch(Exception e) {
fw.append("Error" + e.getMessage() + "\n");
}
}
}
catch(Exception e) {
fw.append("session error " + e.getMessage() + "\n");
}
finally {
fw.close();
ccha.disconnect();
se.disconnect();
}
DeleteCode
File f = new File(path);
if(f.isDirectory()) {
ArrayList allFiles = new ArrayList();
getFiles(f, files); //A simple recursive function which returns all the files in the folder
for(String fpath : files)
new File(fpath).delete();
f.delete();
}
else
f.delete();
Any reference on this would also be of great help !