List of usage examples for org.apache.commons.net.ftp FTPClient getReplyString
public String getReplyString()
From source file:lucee.runtime.exp.FTPException.java
public FTPException(String action, FTPClient client) { super("action [" + action + "] from tag ftp failed", client.getReplyString()); //setAdditional("ReplyCode",Caster.toDouble(client.getReplyCode())); //setAdditional("ReplyMessage",client.getReplyString()); code = client.getReplyCode();/*from w w w .j av a 2 s.c om*/ msg = client.getReplyString(); }
From source file:erigo.filepump.FilePumpWorker.java
/** * utility to create an arbitrary directory hierarchy on the remote ftp server * @param client/*from www. j a v a2s .c om*/ * @param dirTree the directory tree only delimited with / chars. No file name! * @throws Exception */ private static void ftpCreateDirectoryTree(FTPClient client, String dirTree) throws IOException { boolean dirExists = true; //tokenize the string and attempt to change into each directory level. If you cannot, then start creating. String[] directories = dirTree.split("/"); for (String dir : directories) { if (!dir.isEmpty()) { if (dirExists) dirExists = client.changeWorkingDirectory(dir); if (!dirExists) { if (!client.makeDirectory(dir)) throw new IOException("Unable to create remote directory '" + dir + "'. error='" + client.getReplyString() + "'"); if (!client.changeWorkingDirectory(dir)) throw new IOException("Unable to change into newly created remote directory '" + dir + "'. error='" + client.getReplyString() + "'"); } } } }
From source file:com.zxy.commons.net.ftp.FtpUtils.java
/** * FTP handle/*from w ww .j a v a 2 s. co m*/ * * @param <T> return object type * @param ftpConfig ftp config * @param callback ftp callback * @return value */ public static <T> T ftpHandle(FtpConfig ftpConfig, FtpCallback<T> callback) { FTPClient client = null; if (ftpConfig.isFtps() && ftpConfig.getSslContext() != null) { client = new FTPSClient(ftpConfig.getSslContext()); } else if (ftpConfig.isFtps()) { client = new FTPSClient(); } else { client = new FTPClient(); } client.configure(ftpConfig.getFtpClientConfig()); try { // client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); client.connect(ftpConfig.getHost(), ftpConfig.getPort()); client.setConnectTimeout(ftpConfig.getConnectTimeoutMs()); client.setControlKeepAliveTimeout(ftpConfig.getKeepAliveTimeoutSeconds()); if (!Strings.isNullOrEmpty(ftpConfig.getUsername())) { client.login(ftpConfig.getUsername(), ftpConfig.getPassword()); } LOGGER.trace("Connected to {}, reply: {}", ftpConfig.getHost(), client.getReplyString()); // After connection attempt, you should check the reply code to verify success. int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); throw new NetException("FTP server refused connection."); } return callback.process(client); } catch (Exception e) { throw new NetException(e); } finally { if (client.isConnected()) { try { client.logout(); } catch (IOException ioe) { LOGGER.warn(ioe.getMessage()); } try { client.disconnect(); } catch (IOException ioe) { LOGGER.warn(ioe.getMessage()); } } } }
From source file:com.ephesoft.dcma.util.FTPUtil.java
/** * API to create an arbitrary directory hierarchy on the remote ftp server * /*from w w w . j a v a2 s .c o m*/ * @param client {@link FTPClient} the ftp client instance. * @param dirTree the directory tree only delimited with / chars. * @throws IOException if a [problem occurs while making the directory or traversing down the directory structure. */ private static void createFtpDirectoryTree(final FTPClient client, final String dirTree) throws IOException { boolean dirExists = true; /* * tokenize the string and attempt to change into each directory level. If you cannot, then start creating. Needs to be updated * with a proper regex for being the system independent working. NOTE: Cannot use File.seperator here as for windows its value * is "\", which is a special character in terms of regex. */ String[] directories = dirTree.split(DIRECTORY_SEPARATOR); for (String dir : directories) { if (!dir.isEmpty()) { if (dirExists) { dirExists = client.changeWorkingDirectory(dir); } if (!dirExists) { if (!client.makeDirectory(dir)) { throw new IOException(EphesoftStringUtil.concatenate("Unable to create remote directory :", dir, "\t error :", client.getReplyString())); } if (!client.changeWorkingDirectory(dir)) { throw new IOException(EphesoftStringUtil.concatenate( "Unable to change into newly created remote directory :", dir, "\t error :", client.getReplyString())); } } } } }
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.jav a 2s . 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:edu.mda.bioinfo.ids.DownloadFiles.java
private void downloadFromFtpToFile(String theServer, String theServerFile, String theLocalFile) throws IOException, Exception { FTPClient ftp = new FTPClient(); try {/*w w w . jav a 2 s . com*/ int reply = 0; boolean replyB = false; ftp.connect(theServer); System.out.print(ftp.getReplyString()); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); } else { ftp.login("anonymous", "anonymous"); replyB = ftp.setFileType(FTP.BINARY_FILE_TYPE); System.out.print(ftp.getReplyString()); System.out.println("replyB= " + replyB); if (false == replyB) { throw new Exception("Unable to login to " + theServer); } OutputStream output = new FileOutputStream(theLocalFile); replyB = ftp.retrieveFile(theServerFile, output); System.out.print(ftp.getReplyString()); System.out.println("replyB= " + replyB); if (false == replyB) { throw new Exception("Unable to retrieve " + theServerFile); } } ftp.logout(); } catch (IOException rethrownExp) { System.err.println("exception " + rethrownExp.getMessage()); throw rethrownExp; } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ignore) { // do nothing } } } }
From source file:com.haha01haha01.harail.DatabaseDownloader.java
private Boolean downloadFile(String server, int portNumber, String user, String password, String filename, File localFile) throws IOException { FTPClient ftp = null; try {/* www . ja va2 s . co m*/ ftp = new FTPClient(); ftp.setBufferSize(1024 * 1024); ftp.connect(server, portNumber); Log.d(NAME, "Connected. Reply: " + ftp.getReplyString()); if (!ftp.login(user, password)) { return false; } Log.d(NAME, "Logged in"); if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) { return false; } Log.d(NAME, "Downloading"); ftp.enterLocalPassiveMode(); OutputStream outputStream = null; boolean success = false; try { outputStream = new BufferedOutputStream(new FileOutputStream(localFile)); success = ftp.retrieveFile(filename, outputStream); } finally { if (outputStream != null) { outputStream.close(); } } return success; } finally { if (ftp != null) { ftp.logout(); ftp.disconnect(); } } }
From source file:com.streamsets.pipeline.stage.origin.remote.FTPRemoteDownloadSourceDelegate.java
private void setupModTime() { // The FTP protocol's default way to list files gives very inaccurate/ambiguous/inconsistent timestamps (e.g. it's // common for many FTP servers to drop the HH:mm on files older than 6 months). Some FTP servers support the // MDTM command, which returns an accurate/correct timestamp, but not all servers support it. Here, we'll check if // MDTM is supported so we can use it later to get proper timestamps. Unfortunately, VFS does not expose a nice way // to use MDTM or to even get to the underlying FTPClient (VFS-257). We have to use reflection. supportsMDTM = false;/*from w ww . ja v a 2 s .c o m*/ FtpClient ftpClient = null; FtpFileSystem ftpFileSystem = (FtpFileSystem) remoteDir.getFileSystem(); try { ftpClient = ftpFileSystem.getClient(); getFtpClient = ftpClient.getClass().getDeclaredMethod("getFtpClient"); getFtpClient.setAccessible(true); FTPClient rawFtpClient = (FTPClient) getFtpClient.invoke(ftpClient); rawFtpClient.features(); supportsMDTM = rawFtpClient.getReplyString().contains(FTPCmd.MDTM.getCommand()); } catch (Exception e) { LOG.trace("Ignoring Exception when determining MDTM support", e); } finally { if (ftpClient != null) { ftpFileSystem.putClient(ftpClient); } } LOG.info("Using MDTM for more accurate timestamps: {}", supportsMDTM); }
From source file:biz.gabrys.lesscss.extended.compiler.source.FtpSource.java
public String getContent() { final FTPClient connection = connect(); String content = null;//from w ww . ja v a 2 s . c o m final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { if (!connection.retrieveFile(url.getPath(), outputStream)) { throw new SourceException(String.format("Cannot download source \"%s\", reason: %s", url, connection.getReplyString())); } content = outputStream.toString(encoding); } catch (final IOException e) { throw new SourceException(e); } finally { disconnect(connection); IOUtils.closeQuietly(outputStream); } lastModificationDate = getModificationDate(getFile()); return content; }
From source file:com.tobias.vocabulary_trainer.UpdateVocabularies.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.update_vocabularies_layout); prefs = getSharedPreferences(USER_PREFERENCE, Activity.MODE_PRIVATE); serverAdresseEditText = (EditText) findViewById(R.id.server_adresse_edit_text); serverUsernameEditText = (EditText) findViewById(R.id.server_username_edit_text); serverPasswordEditText = (EditText) findViewById(R.id.server_password_edit_text); serverPortEditText = (EditText) findViewById(R.id.server_port_edit_text); serverFileEditText = (EditText) findViewById(R.id.server_file_edit_text); localFileEditText = (EditText) findViewById(R.id.local_file_edit_text); vocabularies = new VocabularyData(this); System.out.println("before updateUIFromPreferences();"); updateUIFromPreferences();//from w w w . j a va2s . c o m System.out.println("after updateUIFromPreferences();"); final Button ServerOkButton = (Button) findViewById(R.id.server_ok); ServerOkButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { System.out.println("ServerOKButton pressed"); savePreferences(); InputStream in; String serverAdresse = serverAdresseEditText.getText().toString(); String serverUsername = serverUsernameEditText.getText().toString(); String serverPassword = serverPasswordEditText.getText().toString(); int serverPort = Integer.parseInt(serverPortEditText.getText().toString()); String serverFile = serverFileEditText.getText().toString(); FTPClient ftp; ftp = new FTPClient(); try { int reply; System.out.println("try to connect to ftp server"); ftp.connect(serverAdresse, serverPort); System.out.print(ftp.getReplyString()); // After connection attempt, you should check the reply code to verify // success. reply = ftp.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { System.out.println("connected to ftp server"); } else { ftp.disconnect(); System.out.println("FTP server refused connection."); } // transfer files System.out.println("try to login"); ftp.login(serverUsername, serverPassword); System.out.println("current working directory: " + ftp.printWorkingDirectory()); System.out.println("try to start downloading"); in = ftp.retrieveFileStream(serverFile); // files transferred //write to database and textfile on sdcard vocabularies.readVocabularies(in, false); ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } } // settings.populateSpinners(); finish(); } }); final Button LocalFileOkButton = (Button) findViewById(R.id.local_file_ok); LocalFileOkButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { File f = new File(Environment.getExternalStorageDirectory(), localFileEditText.getText().toString()); savePreferences(); vocabularies.readVocabularies(f, false); // settings.populateSpinners(); finish(); } }); final Button CancelButton = (Button) findViewById(R.id.Cancel); CancelButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { finish(); } }); vocabularies.close(); }