List of usage examples for org.apache.commons.net.ftp FTPClient getReplyCode
public int getReplyCode()
From source file:com.intellij.diagnostic.SubmitPerformanceReportAction.java
@Nullable private static String uploadFileToFTP(final File reportPath, @NonNls final String ftpSite, @NonNls final String directory, final ProgressIndicator indicator) { FTPClient ftp = new FTPClient(); ftp.setConnectTimeout(30 * 1000);/*from w ww . java 2s .co m*/ try { indicator.setText("Connecting to server..."); ftp.connect(ftpSite); indicator.setText("Connected to server"); if (!ftp.login("anonymous", "anonymous@jetbrains.com")) { return "Failed to login"; } indicator.setText("Logged in"); // After connection attempt, you should check the reply code to verify // success. int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return "FTP server refused connection: " + reply; } if (!ftp.changeWorkingDirectory(directory)) { return "Failed to change directory"; } // else won't work behind FW ftp.enterLocalPassiveMode(); if (!ftp.setFileType(FTPClient.BINARY_FILE_TYPE)) { return "Failed to switch to binary mode"; } indicator.setText("Transferring (" + StringUtil.formatFileSize(reportPath.length()) + ")"); FileInputStream readStream = new FileInputStream(reportPath); try { if (!ftp.storeFile(reportPath.getName(), readStream)) { return "Failed to upload file"; } } catch (IOException e) { return "Error during transfer: " + e.getMessage(); } finally { readStream.close(); } ftp.logout(); return null; } catch (IOException e) { return "Failed to upload: " + e.getMessage(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } } }
From source file:AIR.Common.Web.FileFtpHandler.java
/** * Makes FTP call to the provided URI and retrieves contents * //from w w w.j av a 2s. co m * @param uri * @return ByteArrayInputStream * @throws FtpResourceException */ public static byte[] getBytes(URI uri) throws FtpResourceException { try { FTPClient ftp = new FTPClient(); String[] credentialsAndHost = uri.getAuthority().split("@"); String host = credentialsAndHost[1]; String[] credentials = credentialsAndHost[0].split(":"); ftp.connect(host); ftp.enterLocalPassiveMode(); if (!ftp.login(credentials[0], credentials[1])) { ftp.logout(); ftp.disconnect(); throw new RuntimeException("FTP Authentication Failure"); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.logout(); ftp.disconnect(); throw new RuntimeException("FTP No reponse from server"); } ftp.setFileType(FTPClient.BINARY_FILE_TYPE); ByteArrayOutputStream output = new ByteArrayOutputStream(); ftp.retrieveFile(uri.getPath(), output); output.close(); ftp.logout(); ftp.disconnect(); return output.toByteArray(); } catch (IOException ex) { throw new FtpResourceException(ex); } }
From source file:facturacion.ftp.FtpServer.java
public static InputStream getTokenInputStream(String remote_file_ruta) { FTPClient ftpClient = new FTPClient(); try {/*from ww w . j a v a2 s . c o m*/ //ftpClient.connect("127.0.0.1", 21 ); //ftpClient.login("erpftp", "Tribut@2014"); ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken), Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken))); ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken), Config.getInstance().getProperty(Config.PassFtpToken)); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode(); System.out.println("Respuesta recibida de conexin FTP:" + reply); if (!FTPReply.isPositiveCompletion(reply)) { System.out.println("Imposible conectarse al servidor"); //return -1; } else { System.out.println("se conecto al servidor"); } //ftpClient.enterLocalPassiveMode(); // crear directorio //OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(file_ruta)); //System.out.println("File #1 has been downloaded successfully. 1"); //FileInputStream fis = new FileInputStream("C:\\Users\\aaguerra\\Desktop\\firmado2.p12"); //InputStream is = fis; System.out.println("File rutaqq=" + "1-/" + remote_file_ruta); InputStream is = ftpClient.retrieveFileStream(remote_file_ruta); if (is == null) System.out.println("File #1 es null token"); else System.out.println("File #1 no es null token"); //return ftpClient.retrieveFileStream(remote_file_ruta); return is; } catch (IOException ex) { System.out.println("File #1 has been downloaded successfully. 222"); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { System.out.println("File #1 has been downloaded successfully. 3"); return null; //ex.printStackTrace(); } } return null; }
From source file:com.jsystem.j2autoit.AutoItAgent.java
private static void getFileFtp(String user, String password, String host, int port, String fileName, String location) throws Exception { Log.info("\nretrieve " + fileName + NEW_LINE); FTPClient client = new FTPClient(); client.connect(host, port); //connect to the management ftp server int reply = client.getReplyCode(); // check connection if (!FTPReply.isPositiveCompletion(reply)) { throw new Exception("FTP fail to connect"); }/*from w ww. j a va2 s . com*/ if (!client.login(user, password)) { //check login throw new Exception("FTP fail to login"); } // FileOutputStream fos = null; try { File locationFile = new File(location); File dest = new File(locationFile, fileName); if (dest.exists()) { dest.delete(); } else { locationFile.mkdirs(); } boolean status = client.changeWorkingDirectory("/"); Log.info("chdir-status:" + status + NEW_LINE); client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE); client.setFileType(FTPClient.BINARY_FILE_TYPE); client.enterLocalActiveMode(); InputStream in = client.retrieveFileStream(fileName); if (in == null) { Log.error("Input stream is null\n"); throw new Exception("Fail to retrieve file " + fileName); } Thread.sleep(3000); saveInputStreamToFile(in, new File(location, fileName)); } finally { client.disconnect(); } }
From source file:facturacion.ftp.FtpServer.java
public static int sendImgFile(String fileNameServer, String hostDirServer, InputStream localFile) { FTPClient ftpClient = new FTPClient(); boolean success = false; BufferedInputStream buffIn = null; try {/*from ww w . ja v a2 s .c o m*/ ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken), Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken))); ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken), Config.getInstance().getProperty(Config.PassFtpToken)); ftpClient.enterLocalPassiveMode(); /*ftpClient.connect("127.0.0.1", 21); ftpClient.login("erpftp", "Tribut@2014");*/ ftpClient.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode(); System.out.println("Respuesta recibida de conexin FTP:" + reply); if (!FTPReply.isPositiveCompletion(reply)) { System.out.println("Imposible conectarse al servidor"); return -1; } buffIn = new BufferedInputStream(localFile);//Ruta del archivo para enviar ftpClient.enterLocalPassiveMode(); //crear directorio success = ftpClient.makeDirectory(hostDirServer); System.out.println("sucess 1 = " + success); success = ftpClient.makeDirectory(hostDirServer + "/img"); System.out.println("sucess 233 = " + success); success = ftpClient.storeFile(hostDirServer + "/img/" + fileNameServer, buffIn); System.out.println("sucess 3 = " + success); } catch (IOException ex) { } finally { try { if (ftpClient.isConnected()) { buffIn.close(); //Cerrar envio de arcivos al FTP ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { return -1; //ex.printStackTrace(); } } return (success) ? 1 : 0; }
From source file:facturacion.ftp.FtpServer.java
public static int sendTokenInputStream(String fileNameServer, String hostDirServer, InputStream localFile) { FTPClient ftpClient = new FTPClient(); boolean success = false; BufferedInputStream buffIn = null; try {/* w w w . j ava 2 s.com*/ ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken), Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken))); ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken), Config.getInstance().getProperty(Config.PassFtpToken)); ftpClient.enterLocalPassiveMode(); /*ftpClient.connect("127.0.0.1", 21); ftpClient.login("erpftp", "Tribut@2014");*/ ftpClient.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode(); System.out.println("Respuesta recibida de conexin FTP:" + reply); if (!FTPReply.isPositiveCompletion(reply)) { System.out.println("Imposible conectarse al servidor"); return -1; } buffIn = new BufferedInputStream(localFile);//Ruta del archivo para enviar ftpClient.enterLocalPassiveMode(); //crear directorio System.out.println(hostDirServer); success = ftpClient.makeDirectory(hostDirServer); System.out.println("sucess 1133 = " + success); //showServerReply(ftpClient); success = ftpClient.makeDirectory(hostDirServer + "/token"); /* System.out.println("sucess 1 = "+success); success = ftpClient.makeDirectory("casa111"); System.out.println("sucess 111 = "+success); success = ftpClient.makeDirectory("/usr/erp/token/casa"); System.out.println("sucess 111 = "+success); success = ftpClient.makeDirectory("/casa2"); System.out.println("sucess 1 = "+success); */ success = ftpClient.storeFile(hostDirServer + "/token/" + fileNameServer, buffIn); //success = ftpClient.storeFile("prueba", buffIn); System.out.println("sucess 2 = " + success); //return (success)? 1:0; } catch (IOException ex) { } finally { try { if (ftpClient.isConnected()) { buffIn.close(); //Cerrar envio de arcivos al FTP ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { return -1; //ex.printStackTrace(); } } return (success) ? 1 : 0; }
From source file:com.microsoft.azuretools.utils.WebAppUtils.java
public static FTPClient getFtpConnection(PublishingProfile pp) throws IOException { FTPClient ftp = new FTPClient(); System.out.println("\t\t" + pp.ftpUrl()); System.out.println("\t\t" + pp.ftpUsername()); System.out.println("\t\t" + pp.ftpPassword()); URI uri = URI.create("ftp://" + pp.ftpUrl()); ftp.connect(uri.getHost(), 21);//from w w w. j ava 2 s .com final int replyCode = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { ftp.disconnect(); throw new ConnectException("Unable to connect to FTP server"); } if (!ftp.login(pp.ftpUsername(), pp.ftpPassword())) { throw new ConnectException("Unable to login to FTP server"); } ftp.setControlKeepAliveTimeout(Constants.connection_read_timeout_ms); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode();//Switch to passive mode return ftp; }
From source file:br.com.atmatech.painel.util.EnviaLogs.java
public void enviarBD(File file, String enderecoFTP, String login, String senha) throws IOException { String nomeArquivo = null;// www .j av a 2 s .c o m FTPClient ftp = new FTPClient(); ftp.connect(enderecoFTP); //verifica se conectou com sucesso! if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) { ftp.login(login, senha); } else { //erro ao se conectar ftp.disconnect(); } ftp.changeWorkingDirectory("/atmatech.com.br/ErroLogsPainelFX/"); //abre um stream com o arquivo a ser enviado InputStream is = new FileInputStream(file); //pega apenas o nome do arquivo int idx = file.getName().lastIndexOf(File.separator); if (idx < 0) idx = 0; else idx++; nomeArquivo = file.getName().substring(idx, file.getName().length()); //ajusta o tipo do arquivo a ser enviado ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //faz o envio do arquivo ftp.storeFile(nomeArquivo, is); ftp.disconnect(); }
From source file:de.cismet.cids.custom.utils.formsolutions.FormSolutionFtpClient.java
/** * DOCUMENT ME!/*from w w w . ja v a2s . c om*/ * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ private FTPClient getConnectedFTPClient() throws Exception { final FTPClient ftpClient = new FTPClient(); ftpClient.connect(FormSolutionsProperties.getInstance().getFtpHost()); final int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); throw new Exception("Exception in connecting to FTP Server"); } ftpClient.login(FormSolutionsProperties.getInstance().getFtpLogin(), FormSolutionsProperties.getInstance().getFtpPass()); return ftpClient; }
From source file:ca.nrc.cadc.web.ServerToServerFTPTransfer.java
private FTPClient connect() throws IOException { final FTPClient ftpClient = createFTPClient(); ftpClient.connect(hostname, port);//from ww w .jav a2s. c o m final int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); throw new IOException(String.format("FTP Connection failed with error code %d.", reply)); } else { return login(ftpClient); } }