List of usage examples for org.apache.commons.net.ftp FTPClient enterLocalPassiveMode
public void enterLocalPassiveMode()
PASSIVE_LOCAL_DATA_CONNECTION_MODE
. From source file:ubic.basecode.util.NetUtils.java
/** * Convenient method to get a FTP connection. * /*from w ww .j a v a 2 s.c om*/ * @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:ubic.basecode.util.NetUtils.java
/** * @param f//from w ww . j av a 2 s . co m * @param seekFile * @param outputFile * @param force * @return boolean indicating success or failure. * @throws IOException */ public static boolean ftpDownloadFile(FTPClient f, String seekFile, File outputFile, boolean force) throws IOException { boolean success = false; assert f != null && f.isConnected() : "No FTP connection is available"; f.enterLocalPassiveMode(); long expectedSize = checkForFile(f, seekFile); if (outputFile.exists() && outputFile.length() == expectedSize && !force) { log.info("Output file " + outputFile + " already exists with correct size. Will not re-download"); return true; } OutputStream os = new FileOutputStream(outputFile); log.debug("Seeking file " + seekFile + " with size " + expectedSize + " bytes"); success = f.retrieveFile(seekFile, os); os.close(); if (!success) { throw new IOException("Failed to complete download of " + seekFile); } return success; }
From source file:ubic.basecode.util.NetUtils.java
/** * @param f//from w w w .ja va 2 s . co m * @param seekFile * @param outputFileName * @param force * @return boolean indicating success or failure. * @throws IOException * @throws FileNotFoundException */ public static boolean ftpDownloadFile(FTPClient f, String seekFile, String outputFileName, boolean force) throws IOException, FileNotFoundException { f.enterLocalPassiveMode(); return ftpDownloadFile(f, seekFile, new File(outputFileName), force); }
From source file:ubic.basecode.util.NetUtils.java
/** * Get the size of a remote file./*w ww .ja v a2 s . c o m*/ * * @param f FTPClient * @param seekFile * @return * @throws IOException */ public static long ftpFileSize(FTPClient f, String seekFile) throws IOException { if (f == null || !f.isConnected()) { throw new IOException("No FTP connection"); } f.enterLocalPassiveMode(); int maxTries = 3; for (int i = 0; i < maxTries; i++) { FTPFile[] files = f.listFiles(seekFile); if (files.length == 1) { return files[0].getSize(); } else if (files.length > 1) { throw new IOException(files.length + " files found when expecting one"); } // otherwise keep trying. } throw new FileNotFoundException( "Didn't get expected file information for " + seekFile + " (" + maxTries + " attempts)"); }
From source file:ubicrypt.core.provider.ftp.FTProvider.java
private Observable<FTPClient> connect() { return Observable.<FTPClient>create(subscriber -> { final FTPClient client = new FTPClient(); try {//from ww w .j a va2s. co 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
/** * Do an FTP put of the given bytes// w w w . j a v a 2 s .c o m * * @param server server * @param userName user name on server * @param password password on server * @param destination Where to put the bytes * @param bytes The bytes * * @throws Exception On badness */ public static void ftpPut(String server, String userName, String password, String destination, byte[] bytes) throws Exception { FTPClient f = new FTPClient(); f.connect(server); f.login(userName, password); f.setFileType(FTP.BINARY_FILE_TYPE); f.enterLocalPassiveMode(); checkFtp(f, "Connecting to ftp server"); f.storeFile(destination, new ByteArrayInputStream(bytes)); checkFtp(f, "Storing file"); f.logout(); f.disconnect(); }
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 w w . j a v a 2 s . c o m*/ 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; }
From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java
public static FTPClient ftpConnect(String host, int port, String username, String password) throws IOException { FTPClient ftp = new FTPClient(); try {/*from ww w . j a v a 2 s.c o m*/ ftp.connect(host, port); log.debug("Trying " + host + ":" + port); log.debug(ftp.getReplyString()); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); throw new IOException("FTP server refused connection: " + reply); } else { log.debug("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; }
From source file:uk.ac.manchester.cs.datadesc.validator.utils.UrlReader.java
private InputStream getInputStream(FTPClient ftp) throws VoidValidatorException { try {/* w w w . j a v a2 s .c om*/ // in theory this should not be necessary as servers should default to ASCII // but they don't all do so - see NET-500 ftp.setFileType(FTP.ASCII_FILE_TYPE); ftp.enterLocalPassiveMode(); ftp.setUseEPSVwithIPv4(useEpsvWithIPv4); String path = uri.getPath(); return ftp.retrieveFileStream(path); } catch (FTPConnectionClosedException ex) { disconnect(ftp); throw new VoidValidatorException("Server closed connection.", ex); } catch (IOException ex) { disconnect(ftp); throw new VoidValidatorException("IOEXception with Server ", ex); } }
From source file:uk.sipperfly.utils.FTPUtil.java
public static void reconnect() throws SocketException, IOException { FTPClient ftpClient = new FTPClient(); ftpClient.setControlEncoding("UTF-8"); ftpClient.connect(host, port);//from ww w . jav a 2 s . c o m ftpClient.login(username, password); if (mode.equalsIgnoreCase("passive")) { ftpClient.enterLocalPassiveMode(); } else if (mode.equalsIgnoreCase("active")) { ftpClient.enterLocalActiveMode(); } int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { Logger.getLogger(GACOM).log(Level.INFO, "FTP Login: ".concat(ftpClient.getReplyString())); ftpClient.disconnect(); } ftpClient.setKeepAlive(true); ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE); ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE); ftpClient.setControlKeepAliveTimeout(300); // ftpClient.sendSiteCommand("RECFM=FB"); // ftpClient.sendSiteCommand("LRECL=2000"); // ftpClient.sendSiteCommand("BLKSIZE=27000"); // ftpClient.sendSiteCommand("CY"); // ftpClient.sendSiteCommand("PRI= 50"); // ftpClient.sendSiteCommand("SEC=25"); // ftpClient.sendSiteCommand("RECFM=FB"); // ftpClient.sendSiteCommand("LRECL=2000"); // ftpClient.sendSiteCommand("BLOCKSIZE=27000"); // ftpClient.sendSiteCommand("SPACE=(CYL,(30,300),RLSE)"); // ftpClient.sendSiteCommand("TR"); // ftpClient.sendSiteCommand("PRI=450"); // ftpClient.sendSiteCommand("SEC=4500"); Logger.getLogger(GACOM).log(Level.INFO, "Reconnected FTP"); System.out.println("reconnected"); }