static void downloadAllFiles(String fileType) { try { java.util.Vector fileList = sftpChannel.ls("."); if (fileList != null) { for (int ii = 0; ii < fileList.size(); ii++) { Object obj = fileList.elementAt(ii); LsEntry lsentry = (com.jcraft.jsch.ChannelSftp.LsEntry) obj; if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) { String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getFilename(); if (fileType.equals(".*")) { if (!(lsentry.getAttrs().isDir())) { boolean valid = checkFileName(fileName); if (valid) { totalFileCount++; downloadFileByName(fileName); } } } else if (fileName.toLowerCase().endsWith(fileType)) { boolean valid = checkFileName(fileName); if (valid) { totalFileCount++; downloadFileByName(fileName); } } } } } } catch (Exception e) { System.out.println("Exception in downloadAllFiles(): " + e.toString()); errorMessage += "Exception in downloadAllFiles(): " + e.toString() + "\n"; } }
private static void moveFileToProcessedFolder(String fileName) { try { String newPath = processedDirectory + "/" + fileName; sftpChannel.rename(fileName, newPath); System.out.println("File moved to processed folder"); } catch (Exception e) { System.out.println("Exception in moveFile(" + fileName + "): " + e.toString()); errorMessage += "Exception in moveFile(" + fileName + "): " + e.toString() + "\n"; } }
private static boolean goToServerDirectory() { try { System.out.println("Accessing ServerDirectory : " + serverDirectory); sftpChannel.cd(serverDirectory); System.out.println("Accessing ServerDirectory Completed."); return true; } catch (Exception e) { System.out.println( "Exception in goToServerDirectory(" + serverDirectory + "): " + e.toString()); errorMessage += "Exception in goToServerDirectory(" + serverDirectory + "): " + e.toString() + "\n"; } return false; }
static void downloadFileByName(String filename) { try { System.out.println("Downloading file : " + filename); int mode = ChannelSftp.OVERWRITE; sftpChannel.get(filename, localDirectory, null, mode); downloadedFileCount++; System.out.println("Download Completed."); // file moving to processed folder moveFileToProcessedFolder(filename); } catch (SftpException e) { System.out.println("Exception in downloadFileByName(" + filename + "): " + e.toString()); errorMessage += "Exception in downloadFileByName(" + filename + "): " + e.toString() + "\n"; } catch (Exception e) { System.out.println("Exception in downloadFileByName(" + filename + "): " + e.toString()); errorMessage += "Exception in downloadFileByName(" + filename + "): " + e.toString() + "\n"; } }
private static boolean isDirecotryExist(String processedDirectory) { try { java.util.Vector fileList = sftpChannel.ls("."); if (fileList != null) { for (int ii = 0; ii < fileList.size(); ii++) { Object obj = fileList.elementAt(ii); LsEntry lsentry = (com.jcraft.jsch.ChannelSftp.LsEntry) obj; if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) { String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getFilename(); if (fileName.equals(processedDirectory)) { return true; } } } } } catch (Exception e) { System.out.println("Exception in isDirecotryExist(): " + e.toString()); errorMessage += "Exception in isDirecotryExist(): " + e.toString() + "\n"; } return false; }
public static String loadFilesFromSFTP( String fileName, String localDirectory, String username, String password, String serverPath, int port) throws Exception { int i = 1; if (i == 1) throw new Exception(); try { String[] strArray = splitHostNameAndDirectory(serverPath); if (!((strArray[0].length() > 0) && (strArray[1].length() > 0))) { return "F+Invalid server path.+" + "Error Occurred in loadFilesFromSFTP(): " + errorMessage; } String strHost = strArray[0]; String strServerDirectory = strArray[1]; setProperties(strServerDirectory, localDirectory, username, password, strHost, port); boolean validConnection = getConnection(username, password, strHost, port); if (validConnection) { boolean isAccessible = goToServerDirectory(); if (isAccessible) { if (!isDirecotryExist(processedDirectory)) sftpChannel.mkdir(processedDirectory); getIVRSTrialList(); if (fileName.matches( "\\x2A\\x2E(([\\p{Alnum}]{3,4})|(\\x2A))")) // pattern for [*.txt] or [*.html] // // [*->42(Ox2A) and .->46(Ox2E)] { int dotPos = fileName.toLowerCase().lastIndexOf('.', fileName.length()); String fileType = fileName.toLowerCase().substring(dotPos, fileName.length()); downloadAllFiles(fileType); } else { totalFileCount = 1; downloadFileByName(fileName); } } disconnectFromSFTP(); errorMessageFurnishing(); if ((totalFileCount == 0) && (downloadedFileCount == 0)) { logMessage = "T+" + "Server has no files to download.+" + errorMessage; } else if (totalFileCount != downloadedFileCount) { logMessage = "T+" + downloadedFileCount + " file(s) downloaded out of " + totalFileCount + " files.+" + errorMessage; } else if (totalFileCount == downloadedFileCount) { logMessage = "T+Successfully " + downloadedFileCount + " file(s) downloaded.+" + errorMessage; } } else { logMessage = "F+Connection Error Occurred.+" + errorMessage; } return logMessage; } catch (Exception e) { disconnectFromSFTP(); System.out.println("Exception in loadFilesFromSFTP(): " + e.toString()); errorMessage += "Exception in loadFilesFromSFTP(): " + e.toString(); return "F+Error Occurred in loadFilesFromSFTP()+" + errorMessage; } }