Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void uploadFilesOnFTPServer() {
- try {
- System.out.println("n" + "---------------------------------------------------------------------------------"+ "rn");
- System.out.println("Read FTP Login details from PROPERTIES file"+ "rn");
- System.out.println("---------------------------------------------------------------------------------"+ "rn");
- Properties prop = new Properties();
- InputStream input = null;
- String inputFS = getAutomationInputDataPath()+"//Validation.properties";
- input = new FileInputStream(inputFS);
- prop.load(input);
- //-Input file with test data
- System.out.println("t" + "Uploading data on ftp server"+"rn");
- String ftp_port = prop.getProperty("ftp_port");
- int ftp_host = Integer.parseInt(prop.getProperty("ftp_host"));
- String ftp_username = prop.getProperty("ftp_username");
- String ftp_password = prop.getProperty("ftp_password");
- String server = ftp_port;
- int port = ftp_host;
- String user = ftp_username;
- String pass = ftp_password;
- FTPSClient ftpClient = new FTPSClient();
- //-connect and login to the server
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- //-use local passive mode to pass firewall
- ftpClient.enterLocalPassiveMode();
- System.out.println("Connected");
- String remoteDirPath = "/www/ngage/screenshots";
- String localDirPath = folderPathForUploadingOnFTP;
- uploadDirectory(ftpClient, remoteDirPath, localDirPath, "");
- //-log out and disconnect from the server
- ftpClient.logout();
- ftpClient.disconnect();
- System.out.println("Disconnected");
- } catch (IOException ex) {
- System.err.println("Error occured....."+ex.getMessage());
- ex.printStackTrace();
- }
- }
- public static void uploadDirectory(FTPSClient ftpClient,String remoteDirPath, String localParentDir, String remoteParentDir)
- throws IOException {
- System.out.println("LISTING directory: " + localParentDir);
- File localDir = new File(localParentDir);
- File[] subFiles = localDir.listFiles();
- if (subFiles != null && subFiles.length > 0) {
- for (File item : subFiles) {
- String remoteFilePath = remoteDirPath + "/" + remoteParentDir
- + "/" + item.getName();
- if (remoteParentDir.equals("")) {
- remoteFilePath = remoteDirPath + "/" + item.getName();
- }
- if (item.isFile()) {
- // upload the file
- String localFilePath = item.getAbsolutePath();
- boolean uploaded = uploadSingleFile(ftpClient,
- localFilePath, remoteFilePath);
- if (uploaded) {
- } else {
- System.out.println("COULD NOT upload the file: "
- + localFilePath);
- }
- } else {
- // create directory on the server
- boolean created = ftpClient.makeDirectory(remoteFilePath);
- if (created) {
- System.out.println("CREATED the directory: "
- + remoteFilePath);
- } else {
- System.out.println("COULD NOT create the directory: "
- + remoteFilePath);
- }
- // upload the sub directory
- String parent = remoteParentDir + "/" + item.getName();
- if (remoteParentDir.equals("")) {
- parent = item.getName();
- }
- localParentDir = item.getAbsolutePath();
- uploadDirectory(ftpClient, remoteDirPath, localParentDir,
- parent);
- }
- }
- }ftpClient.makeDirectory(remoteDirPath);
- }
- public static boolean uploadSingleFile(FTPSClient ftpClient,
- String localFilePath, String remoteFilePath) throws IOException {
- File localFile = new File(localFilePath);
- InputStream inputStream = new FileInputStream(localFile);
- try {
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- return ftpClient.storeFile(remoteFilePath, inputStream);
- } finally {
- inputStream.close();
- }
- }
Add Comment
Please, Sign In to add comment