jsch
jsch copied to clipboard
After Renaming file on remote server jsch throws "No Such File" error
trafficstars
Am currently navigating remote sftp server using JSch and i can get the files wth no issues,but the moment i rename the file manually with mv command,i get "No Such File" exception. Exception happens in this code section and inly after renaming the file manually and rerunning the application even after rebooting the machine. sftp.get(sftp.pwd() + "/" + ls.getFilename().trim(),
public void executeSourceTask(String sourcePath) throws Exception {
log.log(Level.INFO, "******************SOURCE START PROCESSING***************************");
if (sftp == null) {
sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
}
if (!session.isConnected()) {
session.connect();
sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
}
SftpATTRS attrs = null;
Vector<LsEntry> v = null;
if (firstTime) {
sftp.cd(sourcePath);
v = sftp.ls(sourcePath);
firstTime = false;
} else {
attrs = sftp.stat(sourcePath);
if (attrs.isDir()) {
sftp.cd(sourcePath);
v = sftp.ls(sourcePath);
}
}
int fileNumber = v == null ? 0 : v.size();
for (int i = 0; i < fileNumber; i++) {
LsEntry ls = v.get(i);
attrs = ls.getAttrs();
//Navigate recursively through folders and subfolders
if (attrs.isDir()) {
if (!ls.getFilename().startsWith(".")) {
executeSourceTask(sourcePath + "/" + ls.getFilename());
}
} else if (!attrs.isDir()) {
if (!ls.getFilename().startsWith(".")) {
if (search_type.trim().equalsIgnoreCase("contains")) {
if (ls.getFilename().trim().contains(fileName.trim())) {
FileOutputStream fos = new FileOutputStream(new File(properties.getString("description") + "/" + ls.getFilename()));
try {
**sftp.get(sftp.pwd() + "/" + ls.getFilename().trim(),**
fos);
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
fos.close();
}
}
} else if (search_type.equalsIgnoreCase("equals")) {
if (ls.getFilename().equals(fileName)) {
sftp.get(ls.getFilename(), properties.getString("description") + File.separatorChar + ls.getFilename());
}
}
}
}
}
log.log(Level.INFO, "******************SOURCE END PROCESSING***************************");
}