List of usage examples for org.apache.commons.net.ftp FTPClient getReplyCode
public int getReplyCode()
From source file:test.java.FTPServerTest.java
@Test public void testConnectFTPServer() throws IOException { FTPClient ftpClient = ConnectionFTP.connectFTP(SERVER1_USER, SERVER1_PASSWORD, SERVER1_ADDRESS); Assert.assertEquals(FTPReply.isPositiveCompletion(ftpClient.getReplyCode()), true); ftpClient = ConnectionFTP.diconnectFTP(ftpClient, SERVER1_ADDRESS); Assert.assertEquals(ftpClient.isConnected(), false); }
From source file:test.java.FTPServerTest.java
@Test public void testExistFileXMLFTPServer() throws IOException { FTPClient ftpClient = ConnectionFTP.connectFTP(SERVER1_USER, SERVER1_PASSWORD, SERVER1_ADDRESS); Assert.assertEquals(FTPReply.isPositiveCompletion(ftpClient.getReplyCode()), true); List<String> listXMLFiles = FTPServerController.findFilesXMLFTPServer(ftpClient); Assert.assertEquals(listXMLFiles.isEmpty(), false); ftpClient = ConnectionFTP.diconnectFTP(ftpClient, SERVER1_ADDRESS); Assert.assertEquals(ftpClient.isConnected(), false); }
From source file:test.java.FTPServerTest.java
@Test public void testSaveFileXMLFTPServerInDB() throws IOException { Server serverSaved = CommonsTest.saveServerTest(SERVER1_USER, SERVER1_PASSWORD, SERVER1_ADDRESS); FTPClient ftpClient = ConnectionFTP.connectFTP(serverSaved.getUser(), serverSaved.getPassword(), serverSaved.getAddress());/*from www . jav a 2 s .c om*/ Assert.assertEquals(FTPReply.isPositiveCompletion(ftpClient.getReplyCode()), true); List<String> listNameFilesXML = FTPServerController.findFilesXMLFTPServer(ftpClient); for (String itemNameFileXML : listNameFilesXML) { ftpClient = ConnectionFTP.diconnectFTP(ftpClient, serverSaved.getAddress()); ftpClient = ConnectionFTP.connectFTP(serverSaved.getUser(), serverSaved.getPassword(), serverSaved.getAddress()); String contentFileXML = FTPServerController.getFileFTPServer(ftpClient, itemNameFileXML); XMLFile xmlFileSavedRecent = CommonsTest.saveXMLFileTest(itemNameFileXML, contentFileXML, serverSaved.getAddress()); XMLFileDAO xmlFileDAO = new XMLFileDAO(); xmlFileDAO.removeXMLFile(xmlFileSavedRecent); Assert.assertEquals(xmlFileDAO.findXMLFileById(xmlFileSavedRecent.getId()), null); } ServerDAO serverDAO = new ServerDAO(); serverDAO.removeServer(serverSaved); Assert.assertEquals(serverDAO.findServerByAddress(serverSaved.getAddress()), null); ftpClient = ConnectionFTP.diconnectFTP(ftpClient, SERVER1_ADDRESS); Assert.assertEquals(ftpClient.isConnected(), false); }
From source file:test.java.FTPServerTest.java
@Test public void testSaveFileXMLFTPServersInDB() throws IOException { List<Server> listServer = new ArrayList<>(); listServer.add(ServerController.createServer(SERVER1_USER, SERVER1_PASSWORD, SERVER1_ADDRESS)); listServer.add(ServerController.createServer(SERVER2_USER, SERVER2_PASSWORD, SERVER2_ADDRESS)); for (Server itemServer : listServer) { Server serverSaved = CommonsTest.saveServerTest(itemServer.getUser(), itemServer.getPassword(), itemServer.getAddress()); FTPClient ftpClient = ConnectionFTP.connectFTP(serverSaved.getUser(), serverSaved.getPassword(), serverSaved.getAddress()); Assert.assertEquals(FTPReply.isPositiveCompletion(ftpClient.getReplyCode()), true); List<String> listNameFilesXML = FTPServerController.findFilesXMLFTPServer(ftpClient); for (String itemNameFileXML : listNameFilesXML) { ftpClient = ConnectionFTP.diconnectFTP(ftpClient, serverSaved.getAddress()); ftpClient = ConnectionFTP.connectFTP(serverSaved.getUser(), serverSaved.getPassword(), serverSaved.getAddress()); String contentFileXML = FTPServerController.getFileFTPServer(ftpClient, itemNameFileXML); XMLFile xmlFileSavedRecent = CommonsTest.saveXMLFileTest(itemNameFileXML, contentFileXML, serverSaved.getAddress()); XMLFileDAO xmlFileDAO = new XMLFileDAO(); xmlFileDAO.removeXMLFile(xmlFileSavedRecent); Assert.assertEquals(xmlFileDAO.findXMLFileById(xmlFileSavedRecent.getId()), null); }//from w ww .j av a 2 s . c om ServerDAO serverDAO = new ServerDAO(); serverDAO.removeServer(serverSaved); Assert.assertEquals(serverDAO.findServerByAddress(serverSaved.getAddress()), null); ftpClient = ConnectionFTP.diconnectFTP(ftpClient, SERVER1_ADDRESS); Assert.assertEquals(ftpClient.isConnected(), false); } }
From source file:testing.FTPClientExample.java
public static final void main(String[] args) { boolean storeFile = false, binaryTransfer = false, error = false, listFiles = false, listNames = false, hidden = false;//from ww w. j a v a 2 s .c o m boolean localActive = false, useEpsvWithIPv4 = false, feat = false, printHash = false; boolean mlst = false, mlsd = false; boolean lenient = false; long keepAliveTimeout = -1; int controlKeepAliveReplyTimeout = -1; int minParams = 5; // listings require 3 params String protocol = null; // SSL protocol String doCommand = null; String trustmgr = null; String proxyHost = null; int proxyPort = 80; String proxyUser = null; String proxyPassword = null; args = new String[100]; args[0] = "-d"; args[1] = "localhost"; args[2] = "adminroot"; args[3] = "adminroot"; args[4] = "RSAPrivate.key"; args[5] = ".\\FTPServer2\\RSAPrivate.key"; int base = 0; OUTER: for (base = 0; base < args.length; base++) { switch (args[base]) { case "-s": storeFile = true; break; case "-a": localActive = true; break; case "-b": binaryTransfer = true; break; case "-c": doCommand = args[++base]; minParams = 3; break; case "-d": mlsd = true; minParams = 3; break; case "-e": useEpsvWithIPv4 = true; break; case "-f": feat = true; minParams = 3; break; case "-h": hidden = true; break; case "-k": keepAliveTimeout = Long.parseLong(args[++base]); break; case "-l": listFiles = true; minParams = 3; break; case "-L": lenient = true; break; case "-n": listNames = true; minParams = 3; break; case "-p": protocol = args[++base]; break; case "-t": mlst = true; minParams = 3; break; case "-w": controlKeepAliveReplyTimeout = Integer.parseInt(args[++base]); break; case "-T": trustmgr = args[++base]; break; case "-PrH": proxyHost = args[++base]; String parts[] = proxyHost.split(":"); if (parts.length == 2) { proxyHost = parts[0]; proxyPort = Integer.parseInt(parts[1]); } break; case "-PrU": proxyUser = args[++base]; break; case "-PrP": proxyPassword = args[++base]; break; case "-#": printHash = true; break; default: break OUTER; } } int remain = args.length - base; if (remain < minParams) // server, user, pass, remote, local [protocol] { System.err.println(USAGE); System.exit(1); } String server = args[base++]; int port = 0; String parts[] = server.split(":"); if (parts.length == 2) { server = parts[0]; port = Integer.parseInt(parts[1]); } String username = args[base++]; String password = args[base++]; String remote = null; if (args.length - base > 0) { remote = args[base++]; } String local = null; if (args.length - base > 0) { local = args[base++]; } final FTPClient ftp; if (protocol == null) { if (proxyHost != null) { System.out.println("Using HTTP proxy server: " + proxyHost); ftp = new FTPHTTPClient(proxyHost, proxyPort, proxyUser, proxyPassword); } else { ftp = new FTPClient(); } } else { FTPSClient ftps; if (protocol.equals("true")) { ftps = new FTPSClient(true); } else if (protocol.equals("false")) { ftps = new FTPSClient(false); } else { String prot[] = protocol.split(","); if (prot.length == 1) { // Just protocol ftps = new FTPSClient(protocol); } else { // protocol,true|false ftps = new FTPSClient(prot[0], Boolean.parseBoolean(prot[1])); } } ftp = ftps; if ("all".equals(trustmgr)) { ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager()); } else if ("valid".equals(trustmgr)) { ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager()); } else if ("none".equals(trustmgr)) { ftps.setTrustManager(null); } } if (printHash) { ftp.setCopyStreamListener(createListener()); } if (keepAliveTimeout >= 0) { ftp.setControlKeepAliveTimeout(keepAliveTimeout); } if (controlKeepAliveReplyTimeout >= 0) { ftp.setControlKeepAliveReplyTimeout(controlKeepAliveReplyTimeout); } ftp.setListHiddenFiles(hidden); // suppress login details ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); try { int reply; if (port > 0) { ftp.connect(server, port); } else { ftp.connect(server); } System.out.println("Connected to " + server + " on " + (port > 0 ? port : ftp.getDefaultPort())); // After connection attempt, you should check the reply code to verify // success. reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); System.exit(1); } } catch (IOException e) { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } } System.err.println("Could not connect to server."); e.printStackTrace(); System.exit(1); } __main: try { if (!ftp.login(username, password)) { ftp.logout(); error = true; break __main; } System.out.println("Remote system is " + ftp.getSystemType()); if (binaryTransfer) { ftp.setFileType(FTP.BINARY_FILE_TYPE); } // Use passive mode as default because most of us are // behind firewalls these days. if (localActive) { ftp.enterLocalActiveMode(); } else { ftp.enterLocalPassiveMode(); } ftp.setUseEPSVwithIPv4(useEpsvWithIPv4); if (storeFile) { InputStream input; input = new FileInputStream(local); ftp.storeFile(remote, input); input.close(); } else if (listFiles) { if (lenient) { FTPClientConfig config = new FTPClientConfig(); config.setLenientFutureDates(true); ftp.configure(config); } for (FTPFile f : ftp.listFiles(remote)) { System.out.println(f.getRawListing()); System.out.println(f.toFormattedString()); } } else if (mlsd) { for (FTPFile f : ftp.mlistDir(remote)) { System.out.println(f.getRawListing()); System.out.println(f.toFormattedString()); } } else if (mlst) { FTPFile f = ftp.mlistFile(remote); if (f != null) { System.out.println(f.toFormattedString()); } } else if (listNames) { for (String s : ftp.listNames(remote)) { System.out.println(s); } } else if (feat) { // boolean feature check if (remote != null) { // See if the command is present if (ftp.hasFeature(remote)) { System.out.println("Has feature: " + remote); } else { if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) { System.out.println("FEAT " + remote + " was not detected"); } else { System.out.println("Command failed: " + ftp.getReplyString()); } } // Strings feature check String[] features = ftp.featureValues(remote); if (features != null) { for (String f : features) { System.out.println("FEAT " + remote + "=" + f + "."); } } else { if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) { System.out.println("FEAT " + remote + " is not present"); } else { System.out.println("Command failed: " + ftp.getReplyString()); } } } else { if (ftp.features()) { // Command listener has already printed the output } else { System.out.println("Failed: " + ftp.getReplyString()); } } } else if (doCommand != null) { if (ftp.doCommand(doCommand, remote)) { // Command listener has already printed the output // for(String s : ftp.getReplyStrings()) { // System.out.println(s); // } } else { System.out.println("Failed: " + ftp.getReplyString()); } } else { OutputStream output; output = new FileOutputStream(local); ftp.retrieveFile(remote, output); output.close(); } ftp.noop(); // check that control connection is working OK ftp.logout(); } catch (FTPConnectionClosedException e) { error = true; System.err.println("Server closed connection."); e.printStackTrace(); } catch (IOException e) { error = true; e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } } } System.exit(error ? 1 : 0); }
From source file:tufts.oki.remoteFiling.RemoteByteStore.java
/** * Get the current length of thsi byte store. * * @author Mark Norton//w ww.j ava 2 s . c o m * * @return The current length of this byte store. */ public long length() throws osid.filing.FilingException { long length = 0; try { FTPClient client = rc.getClient(); client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); // The file to open consists of the root base plus, path to current directory plus name. //String fn = rc.getRootBase() + ((RemoteCabinet)getParent()).separator() + getDisplayName(); //String fn = rc.getRootBase() + "/" + getDisplayName(); String fn = getFullName(); //System.out.println("length - file name to open: " + fn); FTPFile[] replies = client.listFiles(__fileListParser, fn); System.out.println("File Name = " + fn + " replies =" + replies + "Client:" + client.getStatus()); if (replies == null) { System.out.println(client.getReplyCode()); throw new osid.filing.FilingException( "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR); } //System.out.println(client.getReplyCode()); length = replies[0].getSize(); } catch (IOException e) { throw new osid.filing.FilingException( "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR); } return length; }
From source file:ubic.basecode.util.NetUtils.java
/** * Convenient method to get a FTP connection. * //from ww w . ja va 2s . com * @param host * @param login * @param password * @param mode * @return * @throws SocketException * @throws IOException */ public static FTPClient connect(int mode, String host, String loginName, String password) throws SocketException, IOException { FTPClient f = new FTPClient(); f.enterLocalPassiveMode(); f.setBufferSize(32 * 2 ^ 20); boolean success = false; f.connect(host); int reply = f.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) success = f.login(loginName, password); if (!success) { f.disconnect(); throw new IOException("Couldn't connect to " + host); } f.setFileType(mode); log.debug("Connected to " + host); return f; }
From source file:ubicrypt.core.provider.ftp.FTProvider.java
private Observable<FTPClient> connect() { return Observable.<FTPClient>create(subscriber -> { final FTPClient client = new FTPClient(); try {/*w w w . j av a 2 s. c o m*/ client.connect(conf.getHost(), getConf().getPort() == -1 ? 21 : getConf().getPort()); final int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { log.error("FTP server refused connection:" + client.getReplyString()); if (client.isConnected()) { client.disconnect(); } subscriber.onError( new RuntimeException("FTP server refused connection:" + client.getReplyString())); return; } if (!getConf().isAnonymous()) { if (!client.login(getConf().getUsername(), new String(getConf().getPassword()))) { client.disconnect(); log.warn("FTP wrong credentials:" + client.getReplyString()); subscriber.onError(new RuntimeException("FTP wrong credentials")); } } client.setFileType(FTP.BINARY_FILE_TYPE); client.setBufferSize(1 << 64); client.enterLocalPassiveMode(); client.setControlKeepAliveTimeout(60 * 60); //1h if (!isEmpty(conf.getFolder())) { final String directory = startsWith("/", conf.getFolder()) ? conf.getFolder() : "/" + conf.getFolder(); if (!client.changeWorkingDirectory(directory)) { if (!client.makeDirectory(directory)) { disconnect(client); subscriber.onError(new ProviderException(showServerReply(client))); return; } if (!client.changeWorkingDirectory(directory)) { disconnect(client); subscriber.onError(new ProviderException(showServerReply(client))); return; } } } subscriber.onNext(client); subscriber.onCompleted(); } catch (final IOException e) { disconnect(client); subscriber.onError(e); } }).subscribeOn(Schedulers.io()); }
From source file:ucar.unidata.idv.ui.ImageGenerator.java
/** * IS the FtpClient in an ok state. If it isn't then disconnect it and throw and IllegalStateException * * @param f Ftp client/* w ww.ja v a 2s . co m*/ * @param msg Message to use if in error * * @throws Exception On badness */ private static void checkFtp(FTPClient f, String msg) throws Exception { int replyCode = f.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { String reply = f.getReplyString(); f.disconnect(); throw new IllegalStateException("Error with ftp: " + replyCode + " " + msg + "\n" + reply); } }
From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java
public static FTPClient ftpConnect(String host, String username, String password) throws IOException { FTPClient ftp = new FTPClient(); try {//from w ww. j a v a 2 s.c om ftp.connect(host); log.debug("Trying " + host); log.debug(ftp.getReplyString()); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); throw new IOException("FTP server refused connection: " + reply); } else { log.info("Connected"); } ftp.login(username, password); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); } catch (NoRouteToHostException e) { throw new IOException("Couldn't connect to printer: " + e.getMessage(), e); } catch (UnknownHostException e) { throw new IOException("Couldn't connect to printer: " + e.getMessage(), e); } return ftp; }