List of usage examples for org.apache.commons.net.ftp FTPClient FTPClient
public FTPClient()
From source file:ftp.FTPtask.java
/** * Change directory/*from ww w. j av a2 s.co m*/ * @param FTPADDR, ? ? * @param user, * @param password, * @param ChangeFolder, , , /upload */ public static void FTPChangeDir(String FTPADDR, String user, String password, String ChangeFolder) { FTPClient client = new FTPClient(); try { client.connect(FTPADDR); client.login(user, password); int replyCode = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("Connect failed"); return; } boolean success = client.login(user, password); showServerReply(client); if (!success) { System.out.println("Could not login to the server"); return; } // Changes working directory success = client.changeWorkingDirectory(ChangeFolder); showServerReply(client); if (success) { System.out.println("Successfully changed working directory."); } else { System.out.println("Failed to change working directory. See server's reply."); } // logs out client.logout(); client.disconnect(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.isencia.passerelle.actor.ftp.FtpReceiverChannel.java
/** * /*from w w w. j a va 2 s . c o m*/ * @param destFile * @param generator Flushes messages through the channel * (channel close => destFile closed, the generator can only write while channel open) */ public FtpReceiverChannel(File destFile, String server, String username, String password, boolean isBinaryTransfer, boolean isPassiveMode, IMessageExtractor extractor) { super(extractor); this.remote = destFile; //Remote file to read/write this.server = server; this.username = username; this.password = password; this.binaryTransfer = isBinaryTransfer; this.passiveMode = isPassiveMode; ftp = new FTPClient(); }
From source file:com.isencia.message.ftp.FtpReceiverChannel.java
/** * //from w w w .ja v a 2 s. c om * @param destFile * @param generator Flushes messages through the channel * (channel close => destFile closed, the generator can only write while channel open) */ public FtpReceiverChannel(String destFile, String server, String username, String password, boolean isBinaryTransfer, boolean isPassiveMode, IMessageExtractor extractor) { super(extractor); this.remote = destFile; //Remote file to read/write this.server = server; this.username = username; this.password = password; this.binaryTransfer = isBinaryTransfer; this.passiveMode = isPassiveMode; ftp = new FTPClient(); }
From source file:com.google.android.exoplayer.upstream.FtpDataSource.java
public FtpDataSource(int timeout, int bufferSize, TransferListener listener) { this.listener = listener; ftpClient = new FTPClient(); ftpClient.setDataTimeout(timeout);/* www . j av a2s .c o m*/ // ftpClient.setReceieveDataSocketBufferSize(bufferSize); Log.i("ftp", "new FtpDataSource"); }
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 ww w . j a v a2s. c o m 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:com.japp.FtpConfigTest.java
@Before public void setUp() throws Exception { folder = new File(localOutputDirectory); ftp = new FTPClient(); ftp.connect(serverAddress, 3333);/* w w w . j a va 2 s . c o m*/ //login to server if (!ftp.login(userId, password)) { ftp.logout(); } int reply = ftp.getReplyCode(); //FTPReply stores a set of constants for FTP reply codes. if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } //enter passive mode ftp.enterLocalPassiveMode(); //get system name LOGGER.info("Remote system is " + ftp.getSystemType()); ftp.changeWorkingDirectory(remoteDirectory); LOGGER.info("Current directory is " + ftp.printWorkingDirectory()); }
From source file:com.jmeter.alfresco.utils.FtpUtils.java
/** * Upload directory or file.//from w w w .j a v a 2 s .c o m * * @param host the host * @param port the port * @param userName the user name * @param password the password * @param fromLocalDirOrFile the local dir * @param toRemoteDirOrFile the remote dir * @return the string * @throws IOException Signals that an I/O exception has occurred. */ public String uploadDirectoryOrFile(final String host, final int port, final String userName, final String password, final String fromLocalDirOrFile, final String toRemoteDirOrFile) throws IOException { final FTPClient ftpClient = new FTPClient(); String responseMessage = Constants.EMPTY; try { // Connect and login to get the session ftpClient.connect(host, port); final int replyCode = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(replyCode)) { final boolean loginSuccess = ftpClient.login(userName, password); if (loginSuccess) { LOG.debug("Connected to remote host!"); //Use local passive mode to pass fire-wall //In this mode a data connection is made by opening a port on the server for the client to connect //and this is not blocked by fire-wall. ftpClient.enterLocalPassiveMode(); final File localDirOrFileObj = new File(fromLocalDirOrFile); if (localDirOrFileObj.isFile()) { LOG.debug("Uploading file: " + fromLocalDirOrFile); uploadFile(ftpClient, fromLocalDirOrFile, toRemoteDirOrFile + FILE_SEPERATOR_LINUX + localDirOrFileObj.getName()); } else { uploadDirectory(ftpClient, toRemoteDirOrFile, fromLocalDirOrFile, EMPTY); } responseMessage = "Upload completed successfully!"; } else { responseMessage = "Could not login to the remote host!"; } //Log out and disconnect from the server once FTP operation is completed. if (ftpClient.isConnected()) { try { ftpClient.logout(); } catch (IOException ignored) { LOG.error("Ignoring the exception while logging out from remote host: ", ignored); } try { ftpClient.disconnect(); LOG.debug("Disconnected from remote host!"); } catch (IOException ignored) { LOG.error("Ignoring the exception while disconnecting from remote host: ", ignored); } } } else { responseMessage = "Host connection failed!"; } LOG.debug("ResponseMessage:=> " + responseMessage); } catch (IOException ioexcp) { LOG.error("IOException occured in uploadDirectoryOrFile(..): ", ioexcp); throw ioexcp; } return responseMessage; }
From source file:com.alexkasko.netty.ftp.FtpServerTest.java
@Test public void test() throws IOException, InterruptedException { final DefaultCommandExecutionTemplate defaultCommandExecutionTemplate = new DefaultCommandExecutionTemplate( new ConsoleReceiver()); EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override//from w w w.ja v a 2s . co m protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipe = ch.pipeline(); pipe.addLast("decoder", new CrlfStringDecoder()); pipe.addLast("handler", new FtpServerHandler(defaultCommandExecutionTemplate)); } }); b.localAddress(2121).bind(); FTPClient client = new FTPClient(); // https://issues.apache.org/jira/browse/NET-493 client.setBufferSize(0); client.connect("127.0.0.1", 2121); assertEquals(230, client.user("anonymous")); // active assertTrue(client.setFileType(FTP.BINARY_FILE_TYPE)); assertEquals("/", client.printWorkingDirectory()); assertTrue(client.changeWorkingDirectory("/foo")); assertEquals("/foo", client.printWorkingDirectory()); assertTrue(client.listFiles("/foo").length == 0); assertTrue(client.storeFile("bar", new ByteArrayInputStream("content".getBytes()))); assertTrue(client.rename("bar", "baz")); // assertTrue(client.deleteFile("baz")); // passive assertTrue(client.setFileType(FTP.BINARY_FILE_TYPE)); client.enterLocalPassiveMode(); assertEquals("/foo", client.printWorkingDirectory()); assertTrue(client.changeWorkingDirectory("/foo")); assertEquals("/foo", client.printWorkingDirectory()); //TODO make a virtual filesystem that would work with directory //assertTrue(client.listFiles("/foo").length==1); assertTrue(client.storeFile("bar", new ByteArrayInputStream("content".getBytes()))); assertTrue(client.rename("bar", "baz")); // client.deleteFile("baz"); assertEquals(221, client.quit()); try { client.noop(); fail("Should throw exception"); } catch (IOException e) { //expected; } }
From source file:com.clican.pluto.cms.core.service.impl.SiteServiceImpl.java
public FTPClient getFTPClient(ISite site) throws IOException { String url = site.getUrl();//from ww w . j ava2 s . c o m if (url == null) { return null; } if (url.startsWith("ftp://")) { int port = 21; String hostname = null; try { url = url.substring(6); if (url.indexOf(":") != -1) { hostname = url.substring(0, url.indexOf(":")); if (url.endsWith("/")) { port = Integer.parseInt(url.substring(url.indexOf(":") + 1, url.length() - 1)); } else { port = Integer.parseInt(url.substring(url.indexOf(":") + 1)); } } else { if (url.endsWith("/")) { hostname = url.substring(0, url.length() - 1); } else { hostname = url; } } } catch (Exception e) { throw new UnknownHostException(url); } FTPClient client = new FTPClient(); client.connect(hostname, port); if (log.isDebugEnabled()) { log.debug("Connected to " + url + "."); log.debug(client.getReplyString()); } int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); log.warn("FTP server " + url + " refused connection."); return null; } if (StringUtils.isNotEmpty(site.getUsername())) { boolean login = client.login(site.getUsername(), site.getPassword()); if (!login) { client.disconnect(); return null; } } return client; } return null; }
From source file:de.cismet.cids.custom.utils.formsolutions.FormSolutionFtpClient.java
/** * DOCUMENT ME!//w ww. j a va 2 s. c o m * * @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; }