public static String uploadFileToFTPServer ( String hostName , Integer port , String userName , String password , String localFileFullName , String remotePath ) { String result = null ; FTPClient ftpClient = new FTPClient ( ) ; String errorMessage = "Unable to upload file '%s' for FTP server '%s'." ; InputStream inputStream = null ; try { connectAndLoginOnFTPServer ( ftpClient , hostName , port , userName , password ) ; ftpClient . setFileType ( FTP . BINARY_FILE_TYPE ) ; ftpClient . enterLocalPassiveMode ( ) ; //String fullLocalPath = localPath + fileName; File localFile = new File ( localFileFullName ) ; //String fullRemotePath = remotePath + fileName; String remoteFile = remotePath + FilenameUtils . getName ( localFileFullName ) ; inputStream = new FileInputStream ( localFile ) ; boolean uploadFinished = ftpClient . storeFile ( remoteFile , inputStream ) ; if ( uploadFinished ) { result = String . format ( "File '%s' successfully uploaded" , localFileFullName ) ; } else { result = String . format ( "Failed upload '%s' file to FTP server. Got reply: %s" , localFileFullName , ftpClient . getReplyString ( ) ) ; } } catch ( IOException ex ) { throw new RuntimeException ( String . format ( errorMessage , remotePath , hostName ) , ex ) ; } finally { closeInputStream ( inputStream ) ; disconnectAndLogoutFromFTPServer ( ftpClient , hostName ) ; } return result ; }
Upload a given file to FTP server .