List of usage examples for org.apache.commons.net.ftp FTPClient retrieveFile
public boolean retrieveFile(String remote, OutputStream local) throws IOException
From source file:dk.dma.dmiweather.service.FTPLoader.java
/** * Copied the files from DMIs ftp server to the local machine * * @return a Map with a local file and the time the file was created on the FTP server *///from w w w .j a v a 2s. co m private Map<File, Instant> transferFilesIfNeeded(FTPClient client, String directoryName, List<FTPFile> files) throws IOException { File current = new File(tempDirLocation, directoryName); if (newestDirectories.isEmpty()) { // If we just started check if there is data from an earlier run and delete it File temp = new File(tempDirLocation); if (temp.exists()) { File[] oldFolders = temp.listFiles(new PatternFilenameFilter(FOLDER_PATTERN)); if (oldFolders != null) { List<File> foldersToDelete = Lists.newArrayList(oldFolders); foldersToDelete.remove(current); for (File oldFolder : foldersToDelete) { log.info("deleting old GRIB folder {}", oldFolder); deleteRecursively(oldFolder); } } } } if (!current.exists()) { if (!current.mkdirs()) { throw new IOException("Unable to create temp directory " + current.getAbsolutePath()); } } Stopwatch stopwatch = Stopwatch.createStarted(); Map<File, Instant> transferred = new HashMap<>(); for (FTPFile file : files) { File tmp = new File(current, file.getName()); if (tmp.exists()) { long localSize = Files.size(tmp.toPath()); if (localSize != file.getSize()) { log.info("deleting {} local file has size {}, remote is {}", tmp.getName(), localSize, file.getSize()); if (!tmp.delete()) { log.warn("Unable to delete " + tmp.getAbsolutePath()); } } else { // If the file has the right size we assume it was copied correctly (otherwise we needed to hash them) log.info("Reusing already downloaded version of {}", tmp.getName()); transferred.put(tmp, file.getTimestamp().toInstant()); continue; } } if (tmp.createNewFile()) { log.info("downloading {}", tmp.getName()); // this often fails with java.net.ConnectException: Operation timed out int count = 0; while (count++ < MAX_TRIES) { try (FileOutputStream fout = new FileOutputStream(tmp)) { client.retrieveFile(file.getName(), fout); fout.flush(); break; } catch (IOException e) { log.warn(String.format("Failed to transfer file %s, try number %s", file.getName(), count), e); } } } else { throw new IOException("Unable to create temp file on disk."); } transferred.put(tmp, file.getTimestamp().toInstant()); } log.info("transferred weather files in {} ms", stopwatch.stop().elapsed(TimeUnit.MILLISECONDS)); return transferred; }
From source file:com.peter.javaautoupdater.JavaAutoUpdater.java
public static void run(String ftpHost, int ftpPort, String ftpUsername, String ftpPassword, boolean isLocalPassiveMode, boolean isRemotePassiveMode, String basePath, String softwareName, String args[]) {//from w ww. j a v a 2 s. c om System.out.println("jarName=" + jarName); for (String arg : args) { if (arg.toLowerCase().trim().equals("-noautoupdate")) { return; } } JavaAutoUpdater.basePath = basePath; JavaAutoUpdater.softwareName = softwareName; JavaAutoUpdater.args = args; if (!jarName.endsWith(".jar") || jarName.startsWith("JavaAutoUpdater-")) { if (isDebug) { jarName = "test.jar"; } else { return; } } JProgressBarDialog d = new JProgressBarDialog(new JFrame(), "Auto updater", true); d.progressBar.setIndeterminate(true); d.progressBar.setStringPainted(true); d.progressBar.setString("Updating"); // d.addCancelEventListener(this); Thread longRunningThread = new Thread() { public void run() { d.progressBar.setString("checking latest version"); System.out.println("checking latest version"); FTPClient ftp = new FTPClient(); try { ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); ftp.connect(ftpHost, ftpPort); int reply = ftp.getReplyCode(); System.out.println("reply=" + reply + ", " + FTPReply.isPositiveCompletion(reply)); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); JOptionPane.showMessageDialog(null, "FTP server refused connection", "error", JOptionPane.ERROR_MESSAGE); } d.progressBar.setString("connected to ftp"); System.out.println("connected to ftp"); boolean success = ftp.login(ftpUsername, ftpPassword); if (!success) { ftp.disconnect(); JOptionPane.showMessageDialog(null, "FTP login fail, can't update software", "error", JOptionPane.ERROR_MESSAGE); } if (isLocalPassiveMode) { ftp.enterLocalPassiveMode(); } if (isRemotePassiveMode) { ftp.enterRemotePassiveMode(); } FTPFile[] ftpFiles = ftp.listFiles(basePath, new FTPFileFilter() { @Override public boolean accept(FTPFile file) { if (file.getName().startsWith(softwareName)) { return true; } else { return false; } } }); if (ftpFiles.length > 0) { FTPFile targetFile = ftpFiles[ftpFiles.length - 1]; System.out.println("targetFile : " + targetFile.getName() + " , " + targetFile.getSize() + "!=" + new File(jarName).length()); if (!targetFile.getName().equals(jarName) || targetFile.getSize() != new File(jarName).length()) { int r = JOptionPane.showConfirmDialog(null, "Confirm to update to " + targetFile.getName(), "Question", JOptionPane.YES_NO_OPTION); if (r == JOptionPane.YES_OPTION) { //ftp.enterRemotePassiveMode(); d.progressBar.setString("downloading " + targetFile.getName()); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE); d.progressBar.setIndeterminate(false); d.progressBar.setMaximum(100); CopyStreamAdapter streamListener = new CopyStreamAdapter() { @Override public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) { int percent = (int) (totalBytesTransferred * 100 / targetFile.getSize()); d.progressBar.setValue(percent); } }; ftp.setCopyStreamListener(streamListener); try (FileOutputStream fos = new FileOutputStream(targetFile.getName())) { ftp.retrieveFile(basePath + "/" + targetFile.getName(), fos); } catch (IOException e) { e.printStackTrace(); } d.progressBar.setString("restarting " + targetFile.getName()); restartApplication(targetFile.getName()); } } } ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } } }; d.thread = longRunningThread; d.setVisible(true); }
From source file:hydrograph.engine.spark.datasource.utils.FTPUtil.java
public void download(RunFileTransferEntity runFileTransferEntity) { log.debug("Start FTPUtil download"); File filecheck = new File(runFileTransferEntity.getOutFilePath()); if (!(filecheck.exists() && filecheck.isDirectory()) && !(runFileTransferEntity.getOutFilePath().contains("hdfs://"))) { log.error("Invalid output file path. Please provide valid output file path."); throw new RuntimeException("Invalid output path"); }/*from w ww . java2 s . co m*/ boolean fail_if_exist = false; FTPClient ftpClient = new FTPClient(); int retryAttempt = runFileTransferEntity.getRetryAttempt(); int attemptCount = 1; int i = 0; boolean login = false; boolean done = false; for (i = 0; i < retryAttempt; i++) { try { log.info("Connection attempt: " + (i + 1)); if (runFileTransferEntity.getTimeOut() != 0) ftpClient.setConnectTimeout(runFileTransferEntity.getTimeOut()); log.debug("connection details: " + "/n" + "Username: " + runFileTransferEntity.getUserName() + "/n" + "HostName " + runFileTransferEntity.getHostName() + "/n" + "Portno" + runFileTransferEntity.getPortNo()); ftpClient.connect(runFileTransferEntity.getHostName(), runFileTransferEntity.getPortNo()); login = ftpClient.login(runFileTransferEntity.getUserName(), runFileTransferEntity.getPassword()); if (!login) { log.error("Invalid FTP details provided. Please provide correct FTP details."); throw new FTPUtilException("Invalid FTP details"); } ftpClient.enterLocalPassiveMode(); if (runFileTransferEntity.getEncoding() != null) ftpClient.setControlEncoding(runFileTransferEntity.getEncoding()); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); if (runFileTransferEntity.getOutFilePath().contains("hdfs://")) { log.debug("Processing for HDFS output path"); String outputPath = runFileTransferEntity.getOutFilePath(); String s1 = outputPath.substring(7, outputPath.length()); String s2 = s1.substring(0, s1.indexOf("/")); File f = new File("/tmp"); if (!f.exists()) { f.mkdir(); } int index = runFileTransferEntity.getInputFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/").lastIndexOf('/'); String file_name = runFileTransferEntity.getInputFilePath().substring(index + 1); File isfile = new File(runFileTransferEntity.getOutFilePath() + "\\" + file_name); if (runFileTransferEntity.getOverwrite().equalsIgnoreCase("Overwrite If Exists")) { OutputStream outputStream = new FileOutputStream("/tmp/" + file_name); done = ftpClient.retrieveFile(runFileTransferEntity.getInputFilePath(), outputStream); outputStream.close(); } else { if (!(isfile.exists() && !isfile.isDirectory())) { OutputStream outputStream = new FileOutputStream("/tmp/" + file_name); done = ftpClient.retrieveFile(runFileTransferEntity.getInputFilePath(), outputStream); outputStream.close(); } else { fail_if_exist = true; throw new RuntimeException("File already exists"); } } Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://" + s2); FileSystem hdfsFileSystem = FileSystem.get(conf); String s = outputPath.substring(7, outputPath.length()); String hdfspath = s.substring(s.indexOf("/"), s.length()); Path local = new Path("/tmp/" + file_name); Path hdfs = new Path(hdfspath); hdfsFileSystem.copyFromLocalFile(local, hdfs); } else { int index = runFileTransferEntity.getInputFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/").lastIndexOf('/'); String file_name = runFileTransferEntity.getInputFilePath().substring(index + 1); File isfile = new File(runFileTransferEntity.getOutFilePath() + File.separatorChar + file_name); if (runFileTransferEntity.getOverwrite().equalsIgnoreCase("Overwrite If Exists")) { OutputStream outputStream = new FileOutputStream(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/") + "/" + file_name); done = ftpClient.retrieveFile(runFileTransferEntity.getInputFilePath(), (outputStream)); outputStream.close(); } else { if (!(isfile.exists() && !isfile.isDirectory())) { OutputStream outputStream = new FileOutputStream( runFileTransferEntity.getOutFilePath().replaceAll( Matcher.quoteReplacement("\\"), "/") + File.separatorChar + file_name); done = ftpClient.retrieveFile(runFileTransferEntity.getInputFilePath(), outputStream); outputStream.close(); } else { fail_if_exist = true; Log.error("File already exits"); throw new FTPUtilException("File already exists"); } } } } catch (Exception e) { log.error("error while transferring the file", e); if (!login) { log.error("Login "); throw new FTPUtilException("Invalid FTP details"); } if (fail_if_exist) { log.error("File already exists "); throw new FTPUtilException("File already exists"); } try { Thread.sleep(runFileTransferEntity.getRetryAfterDuration()); } catch (Exception e1) { Log.error("Exception occured during sleep"); } catch (Error err) { log.error("fatal error", e); throw new FTPUtilException(err); } continue; } break; } if (i == runFileTransferEntity.getRetryAttempt()) { try { if (ftpClient != null) { ftpClient.logout(); ftpClient.disconnect(); } } catch (Exception e) { Log.error("Exception while closing the ftp client", e); } if (runFileTransferEntity.getFailOnError()) throw new FTPUtilException("File transfer failed "); } log.debug("Finished FTPUtil download"); }
From source file:com.github.carlosrubio.org.apache.tools.ant.taskdefs.optional.net.FTP.java
/** * Retrieve a single file from the remote host. <code>filename</code> may * contain a relative path specification. <p> * * The file will then be retreived using the entire relative path spec - * no attempt is made to change directories. It is anticipated that this * may eventually cause problems with some FTP servers, but it simplifies * the coding.</p>/*from w w w . j ava 2 s . com*/ * @param ftp the ftp client * @param dir local base directory to which the file should go back * @param filename relative path of the file based upon the ftp remote directory * and/or the local base directory (dir) * @throws IOException in unknown circumstances * @throws BuildException if skipFailedTransfers is false * and the file cannot be retrieved. */ protected void getFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException { OutputStream outstream = null; try { File file = getProject().resolveFile(new File(dir, filename).getPath()); if (newerOnly && isUpToDate(ftp, file, resolveFile(filename))) { return; } if (verbose) { log("transferring " + filename + " to " + file.getAbsolutePath()); } File pdir = file.getParentFile(); if (!pdir.exists()) { pdir.mkdirs(); } outstream = new BufferedOutputStream(new FileOutputStream(file)); ftp.retrieveFile(resolveFile(filename), outstream); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { String s = "could not get file: " + ftp.getReplyString(); if (skipFailedTransfers) { log(s, Project.MSG_WARN); skipped++; } else { throw new BuildException(s); } } else { log("File " + file.getAbsolutePath() + " copied from " + server, Project.MSG_VERBOSE); transferred++; if (preserveLastModified) { outstream.close(); outstream = null; FTPFile[] remote = ftp.listFiles(resolveFile(filename)); if (remote.length > 0) { FILE_UTILS.setFileLastModified(file, remote[0].getTimestamp().getTime().getTime()); } } } } finally { FileUtils.close(outstream); } }
From source file:de.ipk_gatersleben.ag_nw.graffiti.services.GUIhelper.java
private static boolean processDownload(final BackgroundTaskStatusProviderSupportingExternalCallImpl status, String downloadURL, String targetFileName, ObjectRef lastStatus, final int thisRun, String server, String remote, final FTPClient ftp) { String username;/*from ww w . j ava2 s .c o m*/ String password; String local; username = "anonymous@" + server; password = "anonymous"; local = targetFileName; final ObjectRef myoutputstream = new ObjectRef(); ftp.addProtocolCommandListener(new ProtocolCommandListener() { public void protocolCommandSent(ProtocolCommandEvent arg0) { // System.out.print("out: " + arg0.getMessage()); status.setCurrentStatusText1("Command: " + arg0.getMessage()); } public void protocolReplyReceived(ProtocolCommandEvent arg0) { // System.out.print("in : " + arg0.getMessage()); status.setCurrentStatusText2("Message: " + arg0.getMessage()); if (myoutputstream.getObject() != null) { String msg = arg0.getMessage(); if (msg.indexOf("Opening BINARY mode") >= 0) { if (msg.indexOf("(") > 0) { msg = msg.substring(msg.indexOf("(") + "(".length()); if (msg.indexOf(" ") > 0) { msg = msg.substring(0, msg.indexOf(" ")); try { long max = Long.parseLong(msg); MyOutputStream os = (MyOutputStream) myoutputstream.getObject(); os.setMaxBytes(max); } catch (Exception e) { System.out.println( "Could not determine file length for detailed progress information"); } } } } } } }); System.out.println("FTP DOWNLOAD: " + downloadURL); try { if (ftp.isConnected()) { status.setCurrentStatusText2("Using open FTP connection"); System.out.println("Reusing open FTP connection"); } else { ftp.connect(server); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); status.setCurrentStatusText1("Can't connect to FTP server"); status.setCurrentStatusText2("ERROR"); return false; } if (!ftp.login("anonymous", "anonymous")) { if (!ftp.login(username, password)) { ftp.disconnect(); status.setCurrentStatusText1("Can't login to FTP server"); status.setCurrentStatusText2("ERROR"); return false; } } status.setCurrentStatusText1("Set Binary Transfer Mode"); ftp.setFileType(FTP.BINARY_FILE_TYPE); status.setCurrentStatusText2("Activate Passive Transfer Mode"); ftp.enterLocalPassiveMode(); } status.setCurrentStatusText1("Start download..."); status.setCurrentStatusText2("Please Wait."); // ftp.listFiles(pathname); OutputStream output = new MyOutputStream(lastStatus, status, new FileOutputStream(local)); myoutputstream.setObject(output); ftp.setRemoteVerificationEnabled(false); long tA = System.currentTimeMillis(); boolean result = ftp.retrieveFile(remote, output); output.close(); long tB = System.currentTimeMillis(); if (!result) { new File(local).delete(); MainFrame.showMessage("Can't download " + downloadURL + ". File not available.", MessageType.INFO); } else { File f = new File(local); System.out.println("Download completed (" + f.getAbsolutePath() + ", " + (f.length() / 1024) + " KB, " + (int) ((f.length() / 1024d / (tB - tA) * 1000d)) + " KB/s)."); } BackgroundTaskHelper.executeLaterOnSwingTask(10000, new Runnable() { public void run() { try { synchronized (GUIhelper.class) { if (runIdx == thisRun) { System.out.println("Disconnect FTP connection"); ftp.disconnect(); } } } catch (Exception err) { ErrorMsg.addErrorMessage(err); } } }); return result; } catch (Exception err) { System.out.println("ERROR: FTP DOWNLOAD ERROR: " + err.getMessage()); if (ftp != null && ftp.isConnected()) { try { System.out.println("Disconnect FTP connection (following error condition)"); ftp.disconnect(); } catch (Exception err2) { ErrorMsg.addErrorMessage(err2); } } return false; } }
From source file:com.thebigbang.ftpclient.FTPOperation.java
/** * will force keep the device turned on for all the operation duration. * @param params//from www.j ava2 s . c o m * @return */ @SuppressLint("Wakelock") @SuppressWarnings("deprecation") @Override protected Boolean doInBackground(FTPBundle... params) { Thread.currentThread().setName("FTPOperationWorker"); for (final FTPBundle bundle : params) { FTPClient ftp = new FTPClient(); PowerManager pw = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); WakeLock w = pw.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FTP Client"); try { // setup ftp connection: InetAddress addr = InetAddress.getByName(bundle.FTPServerHost); //set here the timeout. TimeoutThread timeout = new TimeoutThread(_timeOut, new FTPTimeout() { @Override public void Occurred(TimeoutException e) { bundle.Exception = e; bundle.OperationStatus = FTPOperationStatus.Failed; publishProgress(bundle); } }); timeout.start(); ftp.connect(addr, bundle.FTPServerPort); int reply = ftp.getReplyCode(); timeout.Stop(); if (!FTPReply.isPositiveCompletion(reply)) { throw new IOException("connection refuse"); } ftp.login(bundle.FTPCredentialUsername, bundle.FTPCredentialPassword); if (bundle.OperationType == FTPOperationType.Connect) { bundle.OperationStatus = FTPOperationStatus.Succed; publishProgress(bundle); continue; } ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); w.acquire(); // then switch between enum of operation types. if (bundle.OperationType == FTPOperationType.RetrieveFilesFoldersList) { ftp.changeWorkingDirectory(bundle.RemoteWorkingDirectory); bundle.FilesOnCurrentPath = ftp.listFiles(); bundle.FoldersOnCurrentPath = ftp.listDirectories(); bundle.OperationStatus = FTPOperationStatus.Succed; } else if (bundle.OperationType == FTPOperationType.RetrieveFolderList) { ftp.changeWorkingDirectory(bundle.RemoteWorkingDirectory); bundle.FoldersOnCurrentPath = ftp.listDirectories(); bundle.OperationStatus = FTPOperationStatus.Succed; } else if (bundle.OperationType == FTPOperationType.RetrieveFileList) { ftp.changeWorkingDirectory(bundle.RemoteWorkingDirectory); bundle.FilesOnCurrentPath = ftp.listFiles(); bundle.OperationStatus = FTPOperationStatus.Succed; } else if (bundle.OperationType == FTPOperationType.GetData) { String finalFPFi = bundle.LocalFilePathName; // The remote filename to be downloaded. if (bundle.LocalWorkingDirectory != null && bundle.LocalWorkingDirectory != "") { File f = new File(bundle.LocalWorkingDirectory); f.mkdirs(); finalFPFi = bundle.LocalWorkingDirectory + finalFPFi; } FileOutputStream fos = new FileOutputStream(finalFPFi); // Download file from FTP server String finalFileN = bundle.RemoteFilePathName; if (bundle.RemoteWorkingDirectory != null && bundle.RemoteWorkingDirectory != "") { finalFileN = bundle.RemoteWorkingDirectory + finalFileN; } boolean b = ftp.retrieveFile(finalFileN, fos); if (b) bundle.OperationStatus = FTPOperationStatus.Succed; else bundle.OperationStatus = FTPOperationStatus.Failed; fos.close(); } else if (bundle.OperationType == FTPOperationType.SendData) { InputStream istr = new FileInputStream(bundle.LocalFilePathName); ftp.changeWorkingDirectory(bundle.RemoteWorkingDirectory); Boolean b = ftp.storeFile(bundle.RemoteFilePathName, istr); istr.close(); if (b) bundle.OperationStatus = FTPOperationStatus.Succed; else bundle.OperationStatus = FTPOperationStatus.Failed; } else if (bundle.OperationType == FTPOperationType.DeleteData) { throw new IOException("DeleteData is Not yet implemented"); } ftp.disconnect(); // then finish/return. //publishProgress(bundle); } catch (IOException e) { e.printStackTrace(); bundle.Exception = e; bundle.OperationStatus = FTPOperationStatus.Failed; } try { w.release(); } catch (RuntimeException ex) { ex.printStackTrace(); } publishProgress(bundle); } return true; }
From source file:com.ibm.cics.ca1y.Emit.java
/** * Get the contents of a file from an FTP server. * /*from w ww. j av a 2 s .c o m*/ * @param filename * - * @param server * - * @param useraname * - * @param userpassword * - * @param transfer * - * @param mode * - * @param epsv * - * @param protocol * - * @param trustmgr * - * @param datatimeout * - * @param proxyserver * - * @param proxyusername * - * @param proxypassword * - * @param anonymouspassword * - * @return byte[] representing the retrieved file, null otherwise. */ private static byte[] getFileUsingFTP(String filename, String server, String username, String userpassword, String transfer, String mode, String epsv, String protocol, String trustmgr, String datatimeout, String proxyserver, String proxyusername, String proxypassword, String anonymouspassword) { FTPClient ftp; if (filename == null || server == null) return null; int port = 0; if (server != null) { String parts[] = server.split(":"); if (parts.length == 2) { server = parts[0]; try { port = Integer.parseInt(parts[1]); } catch (Exception _ex) { } } } int proxyport = 0; if (proxyserver != null) { String parts[] = proxyserver.split(":"); if (parts.length == 2) { proxyserver = parts[0]; try { proxyport = Integer.parseInt(parts[1]); } catch (Exception _ex) { } } } if (username == null) { username = "anonymous"; if (userpassword == null) userpassword = anonymouspassword; } if (protocol == null) { if (proxyserver != null) ftp = new FTPHTTPClient(proxyserver, proxyport, proxyusername, proxypassword); else ftp = new FTPClient(); } else { FTPSClient ftps = null; if ("true".equalsIgnoreCase(protocol)) { ftps = new FTPSClient(true); } else if ("false".equalsIgnoreCase(protocol)) { ftps = new FTPSClient(false); } else if (protocol != null) { String parts[] = protocol.split(","); if (parts.length == 1) ftps = new FTPSClient(protocol); else ftps = new FTPSClient(parts[0], Boolean.parseBoolean(parts[1])); } ftp = ftps; if ("all".equalsIgnoreCase(trustmgr)) { ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager()); } else if ("valid".equalsIgnoreCase(trustmgr)) { ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager()); } else if ("none".equalsIgnoreCase(trustmgr)) { ftps.setTrustManager(null); } } if (datatimeout != null) { try { ftp.setDataTimeout(Integer.parseInt(datatimeout)); } catch (Exception _ex) { ftp.setDataTimeout(-1); } } if (port <= 0) { port = ftp.getDefaultPort(); } if (logger.isLoggable(Level.FINE)) { StringBuilder sb = new StringBuilder(); sb.append(Emit.messages.getString("FTPAboutToGetFileFromServer")).append(" - ").append("file name:") .append(filename).append(",server:").append(server).append(":").append(port) .append(",username:").append(username).append(",userpassword:") .append(userpassword != null && !"anonymous".equals(username) ? "<obscured>" : userpassword) .append(",transfer:").append(transfer).append(",mode:").append(mode).append(",epsv:") .append(epsv).append(",protocol:").append(protocol).append(",trustmgr:").append(trustmgr) .append(",datatimeout:").append(datatimeout).append(",proxyserver:").append(proxyserver) .append(":").append(proxyport).append(",proxyusername:").append(proxyusername) .append(",proxypassword:").append(proxypassword != null ? "<obscured>" : null); logger.fine(sb.toString()); } try { if (port > 0) { ftp.connect(server, port); } else { ftp.connect(server); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); logger.warning(Emit.messages.getString("FTPCouldNotConnectToServer")); return null; } } catch (IOException _ex) { logger.warning(Emit.messages.getString("FTPCouldNotConnectToServer")); if (ftp.isConnected()) try { ftp.disconnect(); } catch (IOException _ex2) { } return null; } ByteArrayOutputStream output; try { if (!ftp.login(username, userpassword)) { ftp.logout(); logger.warning(Emit.messages.getString("FTPServerRefusedLoginCredentials")); return null; } if ("binary".equalsIgnoreCase(transfer)) { ftp.setFileType(2); } else { ftp.setFileType(0); } if ("localactive".equalsIgnoreCase(mode)) { ftp.enterLocalActiveMode(); } else { ftp.enterLocalPassiveMode(); } ftp.setUseEPSVwithIPv4("true".equalsIgnoreCase(epsv)); output = new ByteArrayOutputStream(); ftp.retrieveFile(filename, output); output.close(); ftp.noop(); ftp.logout(); } catch (IOException _ex) { logger.warning(Emit.messages.getString("FTPFailedToTransferFile")); if (ftp.isConnected()) try { ftp.disconnect(); } catch (IOException _ex2) { } return null; } return output.toByteArray(); }
From source file:com.o6Systems.utils.net.FTPModule.java
private int executeFtpCommand(String[] args) throws UnknownHostException { boolean storeFile = false, binaryTransfer = false, error = false, listFiles = false, listNames = false, hidden = false;/*from w w w.j av 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; String username = null; String password = null; int base = 0; // Print the command System.out.println("----------FTP MODULE CALL----------"); for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } System.out.println("--------------------"); // for (base = 0; base < args.length; base++) { if (args[base].equals("-s")) { storeFile = true; } else if (args[base].equals("-a")) { localActive = true; } else if (args[base].equals("-A")) { username = "anonymous"; password = System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName(); } else if (args[base].equals("-b")) { binaryTransfer = true; } else if (args[base].equals("-c")) { doCommand = args[++base]; minParams = 3; } else if (args[base].equals("-d")) { mlsd = true; minParams = 3; } else if (args[base].equals("-e")) { useEpsvWithIPv4 = true; } else if (args[base].equals("-f")) { feat = true; minParams = 3; } else if (args[base].equals("-h")) { hidden = true; } else if (args[base].equals("-k")) { keepAliveTimeout = Long.parseLong(args[++base]); } else if (args[base].equals("-l")) { listFiles = true; minParams = 3; } else if (args[base].equals("-L")) { lenient = true; } else if (args[base].equals("-n")) { listNames = true; minParams = 3; } else if (args[base].equals("-p")) { protocol = args[++base]; } else if (args[base].equals("-t")) { mlst = true; minParams = 3; } else if (args[base].equals("-w")) { controlKeepAliveReplyTimeout = Integer.parseInt(args[++base]); } else if (args[base].equals("-T")) { trustmgr = args[++base]; } else if (args[base].equals("-PrH")) { proxyHost = args[++base]; String parts[] = proxyHost.split(":"); if (parts.length == 2) { proxyHost = parts[0]; proxyPort = Integer.parseInt(parts[1]); } } else if (args[base].equals("-PrU")) { proxyUser = args[++base]; } else if (args[base].equals("-PrP")) { proxyPassword = args[++base]; } else if (args[base].equals("-#")) { printHash = true; } else { break; } } int remain = args.length - base; if (username != null) { minParams -= 2; } String server = args[base++]; int port = 0; String parts[] = server.split(":"); if (parts.length == 2) { server = parts[0]; port = Integer.parseInt(parts[1]); } if (username == null) { username = args[base++]; 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."); return 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(); return 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); } else { // 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); } // 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 } } } return error ? 1 : 0; }
From source file:net.yacy.grid.io.assets.FTPStorageFactory.java
public FTPStorageFactory(String server, int port, String username, String password, boolean deleteafterread) throws IOException { this.server = server; this.username = username == null ? "" : username; this.password = password == null ? "" : password; this.port = port; this.deleteafterread = deleteafterread; this.ftpClient = new Storage<byte[]>() { @Override/*from ww w . j a va2 s .c o m*/ public void checkConnection() throws IOException { return; } private FTPClient initConnection() throws IOException { FTPClient ftp = new FTPClient(); ftp.setDataTimeout(3000); ftp.setConnectTimeout(20000); if (FTPStorageFactory.this.port < 0 || FTPStorageFactory.this.port == DEFAULT_PORT) { ftp.connect(FTPStorageFactory.this.server); } else { ftp.connect(FTPStorageFactory.this.server, FTPStorageFactory.this.port); } ftp.enterLocalPassiveMode(); // the server opens a data port to which the client conducts data transfers int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { if (ftp != null) try { ftp.disconnect(); } catch (Throwable ee) { } throw new IOException("bad connection to ftp server: " + reply); } if (!ftp.login(FTPStorageFactory.this.username, FTPStorageFactory.this.password)) { if (ftp != null) try { ftp.disconnect(); } catch (Throwable ee) { } throw new IOException("login failure"); } ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.setBufferSize(8192); return ftp; } @Override public StorageFactory<byte[]> store(String path, byte[] asset) throws IOException { long t0 = System.currentTimeMillis(); FTPClient ftp = initConnection(); try { long t1 = System.currentTimeMillis(); String file = cdPath(ftp, path); long t2 = System.currentTimeMillis(); ftp.enterLocalPassiveMode(); boolean success = ftp.storeFile(file, new ByteArrayInputStream(asset)); long t3 = System.currentTimeMillis(); if (!success) throw new IOException("storage to path " + path + " was not successful (storeFile=false)"); Data.logger.debug("FTPStorageFactory.store ftp store successfull: check connection = " + (t1 - t0) + ", cdPath = " + (t2 - t1) + ", store = " + (t3 - t2)); } catch (IOException e) { throw e; } finally { if (ftp != null) try { ftp.disconnect(); } catch (Throwable ee) { } } return FTPStorageFactory.this; } @Override public Asset<byte[]> load(String path) throws IOException { FTPClient ftp = initConnection(); ByteArrayOutputStream baos = null; byte[] b = null; try { String file = cdPath(ftp, path); baos = new ByteArrayOutputStream(); ftp.retrieveFile(file, baos); b = baos.toByteArray(); if (FTPStorageFactory.this.deleteafterread) try { boolean deleted = ftp.deleteFile(file); FTPFile[] remaining = ftp.listFiles(); if (remaining.length == 0) { ftp.cwd("/"); if (path.startsWith("/")) path = path.substring(1); int p = path.indexOf('/'); if (p > 0) path = path.substring(0, p); ftp.removeDirectory(path); } } catch (Throwable e) { Data.logger.warn("FTPStorageFactory.load failed to remove asset " + path, e); } } catch (IOException e) { throw e; } finally { if (ftp != null) try { ftp.disconnect(); } catch (Throwable ee) { } } return new Asset<byte[]>(FTPStorageFactory.this, b); } @Override public void close() { } private String cdPath(FTPClient ftp, String path) throws IOException { int success_code = ftp.cwd("/"); if (success_code >= 300) throw new IOException("cannot cd into " + path + ": " + success_code); if (path.length() == 0 || path.equals("/")) return ""; if (path.charAt(0) == '/') path = path.substring(1); // we consider that all paths are absolute to / (home) int p; while ((p = path.indexOf('/')) > 0) { String dir = path.substring(0, p); int code = ftp.cwd(dir); if (code >= 300) { // path may not exist, try to create the path boolean success = ftp.makeDirectory(dir); if (!success) throw new IOException("unable to create directory " + dir + " for path " + path); code = ftp.cwd(dir); if (code >= 300) throw new IOException("unable to cwd into directory " + dir + " for path " + path); } path = path.substring(p + 1); } return path; } }; }
From source file:no.imr.sea2data.core.util.FTPUtil.java
/** * Download a single file from the FTP server * * @param ftpClient an instance of org.apache.commons.net.ftp.FTPClient * class./* w ww . j a v a2 s . co m*/ * @param remoteFilePath path of the file on the server * @param localFile path of directory where the file will be stored * @return true if the file was downloaded successfully, false otherwise * @throws IOException if any network or IO error occurred. */ public static boolean retrieveFile(FTPClient ftpClient, String remoteFilePath, String localFile) throws IOException { File downloadFile = new File(localFile); File parentDir = downloadFile.getParentFile(); if (!parentDir.exists()) { parentDir.mkdir(); } try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile))) { ftpClient.setFileType(FTP.BINARY_FILE_TYPE); return ftpClient.retrieveFile(remoteFilePath, outputStream); } catch (IOException ex) { throw ex; } }