List of usage examples for org.apache.commons.net.ftp FTPSClient isConnected
public boolean isConnected()
From source file:examples.ftp.FTPSExample.java
public static final void main(String[] args) throws NoSuchAlgorithmException { int base = 0; boolean storeFile = false, binaryTransfer = false, error = false; String server, username, password, remote, local; String protocol = "SSL"; // SSL/TLS FTPSClient ftps; for (base = 0; base < args.length; base++) { if (args[base].startsWith("-s")) storeFile = true;/*from www.j a va 2 s. com*/ else if (args[base].startsWith("-b")) binaryTransfer = true; else break; } if ((args.length - base) != 5) { System.err.println(USAGE); System.exit(1); } server = args[base++]; username = args[base++]; password = args[base++]; remote = args[base++]; local = args[base]; ftps = new FTPSClient(protocol); ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); try { int reply; ftps.connect(server); System.out.println("Connected to " + server + "."); // After connection attempt, you should check the reply code to verify // success. reply = ftps.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftps.disconnect(); System.err.println("FTP server refused connection."); System.exit(1); } } catch (IOException e) { if (ftps.isConnected()) { try { ftps.disconnect(); } catch (IOException f) { // do nothing } } System.err.println("Could not connect to server."); e.printStackTrace(); System.exit(1); } __main: try { ftps.setBufferSize(1000); if (!ftps.login(username, password)) { ftps.logout(); error = true; break __main; } System.out.println("Remote system is " + ftps.getSystemName()); if (binaryTransfer) ftps.setFileType(FTP.BINARY_FILE_TYPE); // Use passive mode as default because most of us are // behind firewalls these days. ftps.enterLocalPassiveMode(); if (storeFile) { InputStream input; input = new FileInputStream(local); ftps.storeFile(remote, input); input.close(); } else { OutputStream output; output = new FileOutputStream(local); ftps.retrieveFile(remote, output); output.close(); } ftps.logout(); } catch (FTPConnectionClosedException e) { error = true; System.err.println("Server closed connection."); e.printStackTrace(); } catch (IOException e) { error = true; e.printStackTrace(); } finally { if (ftps.isConnected()) { try { ftps.disconnect(); } catch (IOException f) { // do nothing } } } System.exit(error ? 1 : 0); }
From source file:com.claim.controller.FileTransferController.java
public boolean uploadMutiFilesWithFTPS(ObjFileTransfer ftpObj) throws Exception { FTPSClient ftpsClient = null; int replyCode; boolean completed = false; try {/*from www.ja v a 2s. co m*/ FtpProperties properties = new ResourcesProperties().loadFTPProperties(); try { ftpsClient = new FTPSClient(properties.getFtp_protocal(), properties.isFtp_impicit()); //ftpsClient.setAuthValue(ConstantFtp.FTPS_PROTOCAL); ftpsClient.setDataTimeout(ConstantFtp.FTP_TIMEOUT); ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); System.out.print(ftpsClient.getReplyString()); ftpsClient.connect(properties.getFtp_server(), properties.getFtp_port()); FtpUtil.showServerReply(ftpsClient); } catch (ConnectException ex) { FtpUtil.showServerReply(ftpsClient); Console.LOG(ex.getMessage(), 0); System.out.println("ConnectException: " + ex.getMessage()); ex.printStackTrace(); } catch (SocketException ex) { FtpUtil.showServerReply(ftpsClient); Console.LOG(ex.getMessage(), 0); System.out.println("SocketException: " + ex.getMessage()); ex.printStackTrace(); } catch (UnknownHostException ex) { FtpUtil.showServerReply(ftpsClient); Console.LOG(ex.getMessage(), 0); System.out.println("UnknownHostException: " + ex.getMessage()); ex.printStackTrace(); } replyCode = ftpsClient.getReplyCode(); FtpUtil.showServerReply(ftpsClient); if (!FTPReply.isPositiveCompletion(replyCode)) { ftpsClient.disconnect(); Console.LOG("Exception in connecting to FTP Serve ", 0); throw new Exception("Exception in connecting to FTP Server"); } else { Console.LOG("Success in connecting to FTP Serve ", 1); } try { boolean success = ftpsClient.login(properties.getFtp_username(), properties.getFtp_password()); FtpUtil.showServerReply(ftpsClient); if (!success) { throw new Exception("Could not login to the FTP server."); } else { Console.LOG("login to the FTP server. Successfully ", 1); } //ftpClient.enterLocalPassiveMode(); } catch (FTPConnectionClosedException ex) { Console.LOG(ex.getMessage(), 0); FtpUtil.showServerReply(ftpsClient); System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } ftpsClient.setFileType(FTP.BINARY_FILE_TYPE); ftpsClient.execPBSZ(0); //FTP_REMOTE_HOME = ftpClient.printWorkingDirectory(); String workingDirectoryReportType = properties.getFtp_remote_directory() + File.separator + ftpObj.getFtp_report_type(); FtpUtil.ftpCreateDirectoryTree(ftpsClient, workingDirectoryReportType); FtpUtil.showServerReply(ftpsClient); String workingDirectoryStmp = workingDirectoryReportType + File.separator + ftpObj.getFtp_stmp(); FtpUtil.ftpCreateDirectoryTree(ftpsClient, workingDirectoryStmp); FtpUtil.showServerReply(ftpsClient); // APPROACH #2: uploads second file using an OutputStream File files = new File(ftpObj.getFtp_directory_path()); for (File file : files.listFiles()) { if (file.isFile()) { System.out.println("file ::" + file.getName()); InputStream in = new FileInputStream(file); ftpsClient.changeWorkingDirectory(workingDirectoryStmp); completed = ftpsClient.storeFile(file.getName(), in); in.close(); Console.LOG( " " + file.getName() + " ", 1); FtpUtil.showServerReply(ftpsClient); } } Console.LOG(" ?... ", 1); //completed = ftpClient.completePendingCommand(); FtpUtil.showServerReply(ftpsClient); completed = true; ftpsClient.disconnect(); } catch (IOException ex) { Console.LOG(ex.getMessage(), 0); FtpUtil.showServerReply(ftpsClient); System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftpsClient.isConnected()) { ftpsClient.logout(); ftpsClient.disconnect(); } } catch (IOException ex) { FtpUtil.showServerReply(ftpsClient); Console.LOG(ex.getMessage(), 0); ex.printStackTrace(); } } return completed; }
From source file:org.wso2.iot.agent.services.FileDownloadService.java
/** * This method downloads the file using sftp client. * * @param operation - operation object. * @param host - host address. * @param ftpUserName - ftp user name. * @param ftpPassword - ftp password. * @param savingLocation - location in the device to save the file. * @param fileName - name of the file to download. * @param serverPort - ftp server port. * @param fileDirectory - the directory of the file in FTP server. * @throws AndroidAgentException - Android agent exception. *///from w ww. j a v a 2 s . c om private void downloadFileUsingFTPSClient(Operation operation, String host, String ftpUserName, String ftpPassword, String savingLocation, String fileName, int serverPort, String fileDirectory) throws AndroidAgentException { FTPSClient ftpsClient = new FTPSClient(); FileOutputStream fileOutputStream = null; OutputStream outputStream = null; String response; try { ftpsClient.connect(host, serverPort); if (ftpsClient.login(ftpUserName, ftpPassword)) { ftpsClient.enterLocalPassiveMode(); ftpsClient.execPROT("P"); fileOutputStream = new FileOutputStream(savingLocation + File.separator + fileName); outputStream = new BufferedOutputStream(fileOutputStream); ftpsClient.changeWorkingDirectory(fileDirectory); if (ftpsClient.retrieveFile(fileName, outputStream)) { response = "File uploaded to the device successfully ( " + fileName + " )."; operation.setStatus(resources.getString(R.string.operation_value_completed)); } else { response = "File uploaded to the device not completed ( " + fileName + " )."; operation.setStatus(resources.getString(R.string.operation_value_error)); } } else { response = ftpUserName + " - FTP login failed."; operation.setStatus(resources.getString(R.string.operation_value_error)); } operation.setOperationResponse(response); } catch (IOException e) { handleOperationError(operation, fileTransferExceptionCause(e, fileName), e); } finally { try { if (ftpsClient.isConnected()) { ftpsClient.logout(); ftpsClient.disconnect(); } } catch (IOException ignored) { } cleanupStreams(null, outputStream, null, fileOutputStream, null, null, null, null); } }
From source file:org.wso2.iot.agent.services.FileUploadService.java
/** * File upload operation using an FTPS explicit TLS client. * * @param operation - operation object. * @param host - host name./*from w w w. j a v a2 s . com*/ * @param ftpUserName - ftp user name. * @param ftpPassword - ftp password. * @param uploadDirectory - ftp directory to upload file. * @param fileLocation - local file location. * @param serverPort - ftp port to connect. * @throws AndroidAgentException - AndroidAgent exception. */ private void uploadFileUsingFTPSClient(Operation operation, String host, String ftpUserName, String ftpPassword, String uploadDirectory, String fileLocation, int serverPort) throws AndroidAgentException { FTPSClient ftpsClient = new FTPSClient(); String fileName = null; InputStream inputStream = null; Boolean loginResult = false; String response; try { File file = new File(fileLocation); fileName = file.getName(); ftpsClient.connect(host, serverPort); ftpsClient.enterLocalPassiveMode(); loginResult = ftpsClient.login(ftpUserName, ftpPassword); ftpsClient.execPROT("P"); inputStream = new FileInputStream(file); ftpsClient.changeWorkingDirectory(uploadDirectory); if (ftpsClient.storeFile(fileName, inputStream)) { response = "File uploaded from the device completed ( " + fileName + " )."; operation.setStatus(resources.getString(R.string.operation_value_completed)); } else { response = "File uploaded from the device not completed ( " + fileName + " )."; operation.setStatus(resources.getString(R.string.operation_value_error)); } operation.setOperationResponse(response); } catch (IOException e) { if (!loginResult) { response = ftpUserName + " - FTP login failed."; } else { response = fileTransferExceptionCause(e, fileName); } handleOperationError(operation, response, e, resources); } finally { try { if (ftpsClient.isConnected()) { ftpsClient.logout(); ftpsClient.disconnect(); } } catch (IOException ignored) { } cleanupStreams(inputStream, null, null, null, null, null, null, null); } }