List of usage examples for org.apache.commons.net.ftp FTPClient setFileType
public boolean setFileType(int fileType) throws IOException
From source file:adams.gui.chooser.FtpRemoteDirectorySetup.java
/** * Returns a new client for the host/port defined in the options. * * @return the client/* www. ja v a2s . c om*/ */ public FTPClient newClient() { FTPClient result; int reply; try { result = new FTPClient(); result.addProtocolCommandListener(this); result.connect(m_Host); reply = result.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { getLogger().severe("FTP server refused connection: " + reply); } else { if (!result.login(m_User, m_Password.getValue())) { getLogger().severe("Failed to connect to '" + m_Host + "' as user '" + m_User + "'"); } else { if (m_UsePassiveMode) result.enterLocalPassiveMode(); if (m_UseBinaryMode) result.setFileType(FTPClient.BINARY_FILE_TYPE); } } } catch (Exception e) { Utils.handleException(this, "Failed to connect to '" + m_Host + "' as user '" + m_User + "': ", e); result = null; } return result; }
From source file:it.baywaylabs.jumpersumo.twitter.TwitterListener.java
/** * @param host FTP Host name./*w ww. j ava 2 s . c om*/ * @param port FTP port. * @param user FTP User. * @param pswd FTP Password. * @param c Context * @return Downloaded name file or blank list if something was going wrong. */ private String FTPDownloadFile(String host, Integer port, String user, String pswd, Context c) { String result = ""; FTPClient mFTPClient = null; try { mFTPClient = new FTPClient(); // connecting to the host mFTPClient.connect(host, port); // Now check the reply code, if positive mean connection success if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) { // Login using username & password boolean status = mFTPClient.login(user, pswd); mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); mFTPClient.enterLocalPassiveMode(); mFTPClient.changeWorkingDirectory(Constants.DIR_ROBOT_MEDIA); FTPFile[] fileList = mFTPClient.listFiles(); long timestamp = 0l; String nameFile = ""; for (int i = 0; i < fileList.length; i++) { if (fileList[i].isFile() && fileList[i].getTimestamp().getTimeInMillis() > timestamp) { timestamp = fileList[i].getTimestamp().getTimeInMillis(); nameFile = fileList[i].getName(); } } Log.d(TAG, "File da scaricare: " + nameFile); mFTPClient.enterLocalActiveMode(); File folder = new File(Constants.DIR_ROBOT_IMG); OutputStream outputStream = null; boolean success = true; if (!folder.exists()) { success = folder.mkdir(); } try { outputStream = new FileOutputStream(folder.getAbsolutePath() + "/" + nameFile); success = mFTPClient.retrieveFile(nameFile, outputStream); } catch (Exception e) { return e.getMessage(); } finally { if (outputStream != null) { outputStream.close(); } } if (success) { result = nameFile; mFTPClient.deleteFile(nameFile); } } } catch (Exception e) { Log.e(TAG, e.getMessage()); } finally { if (mFTPClient != null) { try { mFTPClient.logout(); mFTPClient.disconnect(); } catch (IOException e) { Log.e(TAG, e.getMessage()); } } } return result; }
From source file:com.ephesoft.dcma.ftp.service.FTPServiceImpl.java
/** * API to download a particular directory from FTP Server. * //from www . j a va 2s.c o m * @param sourceDirName {@link String} - Name of the source directory to be copied. * @param destDirectoryPath {@link String} - Full path where directory need to be copied. * @param retryCounter - Start with zero. * @param isDeletedFTPServerSourceContent - set true for deleted the ftp server content. * @throws FTPDataDownloadException if any error occurs while downloading the file. */ @Override public void downloadDirectory(final String sourceDirName, final String destDirectoryPath, final int numberOfRetryCounter, boolean isDeletedFTPServerSourceContent) throws FTPDataDownloadException { boolean isValid = true; if (sourceDirName == null) { isValid = false; LOGGER.error(var_source_dir); throw new FTPDataDownloadException(var_source_dir); } if (destDirectoryPath == null) { isValid = false; LOGGER.error(var_source_des); throw new FTPDataDownloadException(var_source_des); } if (isValid) { FTPClient client = new FTPClient(); String outputFileName = null; File file = new File(destDirectoryPath); if (!file.exists()) { file.mkdir(); } try { createConnection(client); int reply = client.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { LOGGER.info("Starting File Download..."); } else { LOGGER.error("Invalid Connection to FTP server. Disconnecting from FTP server...."); client.disconnect(); isValid = false; throw new FTPDataDownloadException( "Invalid Connection to FTP server. Disconnecting from FTP server...."); } if (isValid) { client.setFileType(FTP.BINARY_FILE_TYPE); String ftpDirectory = EphesoftStringUtil.concatenate(uploadBaseDir, File.separator, sourceDirName); LOGGER.info("Downloading files from FTP server"); try { FTPUtil.retrieveFiles(client, ftpDirectory, destDirectoryPath); } catch (IOException e) { int retryCounter = numberOfRetryCounter; LOGGER.info("Retrying download Attempt#-" + (retryCounter + 1)); if (retryCounter < numberOfRetries) { retryCounter = retryCounter + 1; downloadDirectory(sourceDirName, destDirectoryPath, retryCounter, isDeletedFTPServerSourceContent); } else { LOGGER.error("Error in getting file from FTP server :" + outputFileName); } } if (isDeletedFTPServerSourceContent) { FTPUtil.deleteExistingFTPData(client, ftpDirectory, true); } } } catch (FileNotFoundException e) { LOGGER.error("Error in generating output Stream for file :" + outputFileName); } catch (SocketException e) { LOGGER.error("Could not connect to FTP Server-" + ftpServerURL + e); } catch (IOException e) { LOGGER.error("Could not connect to FTP Server-" + ftpServerURL + e); } finally { try { client.disconnect(); } catch (IOException e) { LOGGER.error("Error in disconnecting from FTP server." + e); } } } }
From source file:adams.core.io.lister.FtpDirectoryLister.java
/** * Returns a new client for the host defined in the options. * * @return the client, null if failed to create *///from w ww . jav a 2 s .c om protected FTPClient newClient() { FTPClient result; int reply; try { result = new FTPClient(); result.addProtocolCommandListener(this); result.connect(m_Host); reply = result.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { getLogger().severe("FTP server refused connection: " + reply); } else { if (!result.login(m_User, m_Password.getValue())) { getLogger().severe("Failed to connect to '" + m_Host + "' as user '" + m_User + "'"); } else { if (m_UsePassiveMode) result.enterLocalPassiveMode(); if (m_UseBinaryMode) result.setFileType(FTPClient.BINARY_FILE_TYPE); } } } catch (Exception e) { Utils.handleException(this, "Failed to connect to '" + m_Host + "' as user '" + m_User + "': ", e); result = null; } return result; }
From source file:com.maxl.java.aips2sqlite.AllDown.java
public void downDesitin() { String fl = ""; String fp = ""; String fs = ""; try {//from ww w .j av a2s.c o m FileInputStream access = new FileInputStream(Constants.DIR_DESITIN + "/access.ami.csv"); BufferedReader br = new BufferedReader(new InputStreamReader(access, "UTF-8")); String line; while ((line = br.readLine()) != null) { // Semicolon is used as a separator String[] gln = line.split(";"); if (gln.length > 2) { if (gln[0].equals("ftp_amiko")) { fl = gln[0]; fp = gln[1]; fs = gln[2]; } } } br.close(); } catch (Exception e) { e.printStackTrace(); } FTPClient ftp_client = new FTPClient(); try { ftp_client.connect(fs, 21); ftp_client.login(fl, fp); ftp_client.enterLocalPassiveMode(); ftp_client.setFileType(FTP.BINARY_FILE_TYPE); System.out.println("- Connected to server " + fs + "..."); // Set working directory String working_dir = "ywesee_in"; ftp_client.changeWorkingDirectory(working_dir); int reply = ftp_client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp_client.disconnect(); System.err.println("FTP server refused connection."); return; } // Get list of filenames FTPFile[] ftpFiles = ftp_client.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { // ... then download all csv files for (FTPFile f : ftpFiles) { String remote_file = f.getName(); if (remote_file.endsWith("csv")) { String local_file = remote_file; if (remote_file.startsWith("Kunden")) local_file = Constants.FILE_CUST_DESITIN; if (remote_file.startsWith("Artikel")) local_file = Constants.FILE_ARTICLES_DESITIN; OutputStream os = new FileOutputStream(Constants.DIR_DESITIN + "/" + local_file); System.out.print("- Downloading " + remote_file + " from server " + fs + "... "); boolean done = ftp_client.retrieveFile(remote_file, os); if (done) System.out.println("success."); else System.out.println("error."); os.close(); } } } } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftp_client.isConnected()) { ftp_client.logout(); ftp_client.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:com.maxl.java.aips2sqlite.AllDown.java
public void downZurRose() { String fl = ""; String fp = ""; String fs = ""; try {/*from w w w. j av a 2s .com*/ FileInputStream access = new FileInputStream(Constants.DIR_ZURROSE + "/access.ami.csv"); BufferedReader br = new BufferedReader(new InputStreamReader(access, "UTF-8")); String line; while ((line = br.readLine()) != null) { // Semicolon is used as a separator String[] gln = line.split(";"); if (gln.length > 2) { if (gln[0].equals("P_ywesee")) { fl = gln[0]; fp = gln[1]; fs = gln[2]; } } } br.close(); } catch (Exception e) { e.printStackTrace(); } FTPClient ftp_client = new FTPClient(); try { ftp_client.connect(fs, 21); ftp_client.login(fl, fp); ftp_client.enterLocalPassiveMode(); ftp_client.setFileType(FTP.BINARY_FILE_TYPE); System.out.println("- Connected to server " + fs + "..."); String[] working_dir = { "ywesee out", "../ywesee in" }; for (int i = 0; i < working_dir.length; ++i) { // Set working directory ftp_client.changeWorkingDirectory(working_dir[i]); int reply = ftp_client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp_client.disconnect(); System.err.println("FTP server refused connection."); return; } // Get list of filenames FTPFile[] ftpFiles = ftp_client.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { // ... then download all csv files for (FTPFile f : ftpFiles) { String remote_file = f.getName(); if (remote_file.endsWith("csv")) { String local_file = remote_file; if (remote_file.startsWith("Artikelstamm")) local_file = Constants.CSV_FILE_DISPO_ZR; OutputStream os = new FileOutputStream(Constants.DIR_ZURROSE + "/" + local_file); System.out.print("- Downloading " + remote_file + " from server " + fs + "... "); boolean done = ftp_client.retrieveFile(remote_file, os); if (done) System.out.println("success."); else System.out.println("error."); os.close(); } } } } } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftp_client.isConnected()) { ftp_client.logout(); ftp_client.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:com.maxl.java.aips2sqlite.AllDown.java
public void downIBSA() { String fl = ""; String fp = ""; String fs = ""; try {//from w w w . java2s.c o m FileInputStream glnCodesCsv = new FileInputStream(Constants.DIR_IBSA + "/access.ami.csv"); BufferedReader br = new BufferedReader(new InputStreamReader(glnCodesCsv, "UTF-8")); String line; while ((line = br.readLine()) != null) { // Semicolon is used as a separator String[] gln = line.split(";"); if (gln.length > 2) { if (gln[0].equals("IbsaAmiko")) { fl = gln[0]; fp = gln[1]; fs = gln[2]; } } } br.close(); } catch (Exception e) { e.printStackTrace(); } FTPClient ftp_client = new FTPClient(); try { ftp_client.connect(fs, 21); ftp_client.login(fl, fp); ftp_client.enterLocalPassiveMode(); ftp_client.changeWorkingDirectory("data"); ftp_client.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftp_client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp_client.disconnect(); System.err.println("FTP server refused connection."); return; } System.out.println("- Connected to server " + fs + "..."); //get list of filenames FTPFile[] ftpFiles = ftp_client.listFiles(); List<String> list_remote_files = Arrays.asList("Konditionen.csv", "Targeting_diff.csv", "Address.csv"); List<String> list_local_files = Arrays.asList(Constants.FILE_CUST_IBSA, Constants.FILE_TARG_IBSA, Constants.FILE_MOOS_ADDR); if (ftpFiles != null && ftpFiles.length > 0) { int index = 0; for (String remote_file : list_remote_files) { OutputStream os = new FileOutputStream(Constants.DIR_IBSA + "/" + list_local_files.get(index)); System.out.print("- Downloading " + remote_file + " from server " + fs + "... "); boolean done = ftp_client.retrieveFile(remote_file, os); if (done) System.out.println("file downloaded successfully."); else System.out.println("error."); os.close(); index++; } } } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftp_client.isConnected()) { ftp_client.logout(); ftp_client.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:com.tumblr.maximopro.iface.router.FTPHandler.java
@Override public byte[] invoke(Map<String, ?> metaData, byte[] data) throws MXException { byte[] encodedData = super.invoke(metaData, data); this.metaData = metaData; FTPClient ftp; if (enableSSL()) { FTPSClient ftps = new FTPSClient(isImplicit()); ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager()); ftp = ftps;//from w w w . j a v a 2s . c o m } else { ftp = new FTPClient(); } InputStream is = null; try { if (getTimeout() > 0) { ftp.setDefaultTimeout(getTimeout()); } if (getBufferSize() > 0) { ftp.setBufferSize(getBufferSize()); } if (getNoDelay()) { ftp.setTcpNoDelay(getNoDelay()); } ftp.connect(getHostname(), getPort()); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } if (!ftp.login(getUsername(), getPassword())) { ftp.logout(); ftp.disconnect(); } ftp.setFileType(FTP.BINARY_FILE_TYPE); if (enableActive()) { ftp.enterLocalActiveMode(); } else { ftp.enterLocalPassiveMode(); } is = new ByteArrayInputStream(encodedData); String remoteFileName = getFileName(metaData); ftp.changeWorkingDirectory("/"); if (createDirectoryStructure(ftp, getDirName().split("/"))) { ftp.storeFile(remoteFileName, is); } else { throw new MXApplicationException("iface", "cannotcreatedir"); } ftp.logout(); } catch (MXException e) { throw e; } catch (SocketException e) { throw new MXApplicationException("iface", "ftpsocketerror", e); } catch (IOException e) { throw new MXApplicationException("iface", "ftpioerror", e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { throw new MXApplicationException("iface", "ftpioerror", e); } } if (ftp != null && ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { throw new MXApplicationException("iface", "ftpioerror", e); } } } return null; }
From source file:net.audumla.climate.bom.BOMDataLoader.java
private synchronized FTPClient getFTPClient(String host) { FTPClient ftp = ftpClients.get(host); if (ftp == null || !ftp.isAvailable() || !ftp.isConnected()) { ftp = new FTPClient(); FTPClientConfig config = new FTPClientConfig(); ftp.configure(config);//from ww w. j a v a 2s .co m try { ftp.setControlKeepAliveTimeout(30); ftp.setControlKeepAliveReplyTimeout(5); ftp.setDataTimeout(3000); ftp.setDefaultTimeout(1000); int reply; ftp.connect(host); LOG.debug("Connected to " + host); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); LOG.error("FTP server '" + host + "' refused connection."); } else { if (!ftp.login("anonymous", "guest")) { LOG.error("Unable to login to server " + host); } ftp.setSoTimeout(60000); ftp.enterLocalPassiveMode(); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); } } catch (IOException e) { LOG.error("Unable to connect to " + host, e); } ftpClients.put(host, ftp); } if (!ftp.isConnected() || !ftp.isAvailable()) { throw new UnsupportedOperationException("Cannot connect to " + host); } return ftp; }
From source file:lucee.runtime.tag.Ftp.java
/** * copy a local file to server//from w ww . j av a 2 s .c o m * @return FTPClient * @throws IOException * @throws PageException */ private FTPClient actionPutFile() throws IOException, PageException { required("remotefile", remotefile); required("localfile", localfile); FTPClient client = getClient(); Resource local = ResourceUtil.toResourceExisting(pageContext, localfile);//new File(localfile); // if(failifexists && local.exists()) throw new ApplicationException("File ["+local+"] already exist, if you want to overwrite, set attribute failIfExists to false"); InputStream is = null; try { is = IOUtil.toBufferedInputStream(local.getInputStream()); client.setFileType(getType(local)); client.storeFile(remotefile, is); } finally { IOUtil.closeEL(is); } writeCfftp(client); return client; }