Guest User

Untitled

a guest
Apr 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. public static void uploadFilesOnFTPServer() {
  2. try {
  3. System.out.println("n" + "---------------------------------------------------------------------------------"+ "rn");
  4. System.out.println("Read FTP Login details from PROPERTIES file"+ "rn");
  5. System.out.println("---------------------------------------------------------------------------------"+ "rn");
  6.  
  7. Properties prop = new Properties();
  8. InputStream input = null;
  9.  
  10. String inputFS = getAutomationInputDataPath()+"//Validation.properties";
  11. input = new FileInputStream(inputFS);
  12. prop.load(input);
  13.  
  14. //-Input file with test data
  15. System.out.println("t" + "Uploading data on ftp server"+"rn");
  16. String ftp_port = prop.getProperty("ftp_port");
  17. int ftp_host = Integer.parseInt(prop.getProperty("ftp_host"));
  18. String ftp_username = prop.getProperty("ftp_username");
  19. String ftp_password = prop.getProperty("ftp_password");
  20.  
  21. String server = ftp_port;
  22. int port = ftp_host;
  23. String user = ftp_username;
  24. String pass = ftp_password;
  25.  
  26. FTPSClient ftpClient = new FTPSClient();
  27. //-connect and login to the server
  28. ftpClient.connect(server, port);
  29. ftpClient.login(user, pass);
  30.  
  31. //-use local passive mode to pass firewall
  32. ftpClient.enterLocalPassiveMode();
  33. System.out.println("Connected");
  34. String remoteDirPath = "/www/ngage/screenshots";
  35. String localDirPath = folderPathForUploadingOnFTP;
  36. uploadDirectory(ftpClient, remoteDirPath, localDirPath, "");
  37. //-log out and disconnect from the server
  38. ftpClient.logout();
  39. ftpClient.disconnect();
  40.  
  41. System.out.println("Disconnected");
  42. } catch (IOException ex) {
  43. System.err.println("Error occured....."+ex.getMessage());
  44. ex.printStackTrace();
  45. }
  46. }
  47.  
  48. public static void uploadDirectory(FTPSClient ftpClient,String remoteDirPath, String localParentDir, String remoteParentDir)
  49. throws IOException {
  50.  
  51. System.out.println("LISTING directory: " + localParentDir);
  52.  
  53. File localDir = new File(localParentDir);
  54. File[] subFiles = localDir.listFiles();
  55. if (subFiles != null && subFiles.length > 0) {
  56. for (File item : subFiles) {
  57. String remoteFilePath = remoteDirPath + "/" + remoteParentDir
  58. + "/" + item.getName();
  59. if (remoteParentDir.equals("")) {
  60. remoteFilePath = remoteDirPath + "/" + item.getName();
  61. }
  62.  
  63. if (item.isFile()) {
  64. // upload the file
  65. String localFilePath = item.getAbsolutePath();
  66. boolean uploaded = uploadSingleFile(ftpClient,
  67. localFilePath, remoteFilePath);
  68. if (uploaded) {
  69. } else {
  70. System.out.println("COULD NOT upload the file: "
  71. + localFilePath);
  72. }
  73. } else {
  74. // create directory on the server
  75. boolean created = ftpClient.makeDirectory(remoteFilePath);
  76. if (created) {
  77. System.out.println("CREATED the directory: "
  78. + remoteFilePath);
  79. } else {
  80. System.out.println("COULD NOT create the directory: "
  81. + remoteFilePath);
  82. }
  83.  
  84. // upload the sub directory
  85. String parent = remoteParentDir + "/" + item.getName();
  86. if (remoteParentDir.equals("")) {
  87. parent = item.getName();
  88. }
  89.  
  90. localParentDir = item.getAbsolutePath();
  91. uploadDirectory(ftpClient, remoteDirPath, localParentDir,
  92. parent);
  93. }
  94. }
  95. }ftpClient.makeDirectory(remoteDirPath);
  96. }
  97.  
  98. public static boolean uploadSingleFile(FTPSClient ftpClient,
  99. String localFilePath, String remoteFilePath) throws IOException {
  100. File localFile = new File(localFilePath);
  101.  
  102. InputStream inputStream = new FileInputStream(localFile);
  103. try {
  104. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  105. return ftpClient.storeFile(remoteFilePath, inputStream);
  106. } finally {
  107. inputStream.close();
  108. }
  109. }
Add Comment
Please, Sign In to add comment