List of usage examples for org.apache.commons.net.ftp FTPClient setFileType
public boolean setFileType(int fileType) throws IOException
From source file:ca.efendi.datafeeds.messaging.FtpSubscriptionMessageListener.java
public void fetch(final FtpSubscription ftpSubscription) { if (_log.isDebugEnabled()) { _log.debug("fetching " + ftpSubscription); }/*from www. j a v a2 s . c o m*/ final FTPClient ftp = new FTPClient(); ftp.setControlKeepAliveTimeout(30); ftp.setControlKeepAliveReplyTimeout(30); ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); try { int reply; ftp.connect(ftpSubscription.getFtpHost()); _log.debug("Connected to " + ftpSubscription.getFtpHost() + " on " + ftp.getDefaultPort()); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); System.exit(1); } } catch (final IOException e) { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (final IOException f) { // do nothing } } System.err.println("Could not connect to server."); e.printStackTrace(); System.exit(1); } boolean error = false; __main: try { if (!ftp.login(ftpSubscription.getFtpUser(), ftpSubscription.getFtpPassword())) { ftp.logout(); error = true; break __main; } _log.info("Remote system is " + ftp.getSystemType()); ftp.setFileType(FTP.BINARY_FILE_TYPE); //ftp.enterLocalActiveMode(); ftp.enterLocalPassiveMode(); //final FTPClientConfig config = new FTPClientConfig(); ////config.setLenientFutureDates(true); //ftp.configure(config); if (!StringUtils.isBlank(ftpSubscription.getFtpFolder())) { ftp.changeWorkingDirectory(ftpSubscription.getFtpFolder()); } final InputStream is = ftp.retrieveFileStream(ftpSubscription.getFtpFile()); if (is == null) { _log.error("FIle not found: " + ftp.getSystemType()); } else { unzip(ftpSubscription, is); is.close(); } ftp.completePendingCommand(); } catch (final FTPConnectionClosedException e) { error = true; System.err.println("Server closed connection."); e.printStackTrace(); } catch (final IOException e) { error = true; e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (final IOException e) { _log.error(e); } } } }
From source file:com.netpace.aims.controller.application.WapApplicationHelper.java
public static boolean wapFTPZipFile(File transferFile) throws AimsException { log.debug("wapFTPZipFile FTP Start. FileName: " + transferFile.getName()); boolean loginStatus = false; boolean dirChanged = false; FileInputStream transferStream = null; FTPClient ftpClient = new FTPClient(); ConfigEnvProperties envProperties = ConfigEnvProperties.getInstance(); //ftp server config String ftpServerAddress = envProperties.getProperty("wap.images.ftp.server.address"); String ftpUser = envProperties.getProperty("wap.images.ftp.user.name"); String ftpPassword = envProperties.getProperty("wap.images.ftp.user.password"); String ftpWorkingDirectory = envProperties.getProperty("wap.images.ftp.working.dir"); //general exception for ftp transfer AimsException aimsException = new AimsException("Error"); aimsException.addException(new AimsException("error.wap.app.ftp.transfer")); String errorMessage = ""; boolean transfered = false; try {//from w w w .j av a 2s . com ftpClient.connect(ftpServerAddress); loginStatus = ftpClient.login(ftpUser, ftpPassword); log.debug("Connection to server " + ftpServerAddress + " " + (loginStatus ? "success" : "failure")); if (loginStatus) { dirChanged = ftpClient.changeWorkingDirectory(ftpWorkingDirectory); log.debug("change remote directory to " + ftpWorkingDirectory + ": " + dirChanged); if (dirChanged) { transferStream = new FileInputStream(transferFile); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); transfered = ftpClient.storeFile(transferFile.getName(), transferStream); log.debug(transferFile.getName() + " transfered: " + transfered + " on server: " + ftpServerAddress); if (!transfered) { errorMessage = "File: " + transferFile.getName() + " not transfered to " + (ftpServerAddress + "/" + ftpServerAddress); System.out.println("Error in FTP Transfer: " + errorMessage); sendFTPErrorMail(errorMessage); throw aimsException; } } else { errorMessage = "Directory: " + ftpWorkingDirectory + " not found in " + ftpServerAddress; System.out.println("Error in FTP Transfer: " + errorMessage); sendFTPErrorMail(errorMessage); throw aimsException; } } //end loginstatus else { errorMessage = "FTP Authentication failed. \n\nServer: " + ftpServerAddress + "\nUserID: " + ftpUser + "\nPassword: " + ftpPassword; System.out.println("Error in FTP Transfer: " + errorMessage); sendFTPErrorMail(errorMessage); throw aimsException; } } //end try catch (FileNotFoundException e) { System.out.println("Exception: " + transferFile.getName() + " not found in temp directory"); e.printStackTrace();//zip file not found throw aimsException; } catch (IOException ioe) { System.out.println("Exception: Error in connection " + ftpServerAddress); ioe.printStackTrace(); sendFTPErrorMail("Error in connection to : " + ftpServerAddress + "\n\n" + MiscUtils.getExceptionStackTraceInfo(ioe)); throw aimsException; } finally { try { //close stream if (transferStream != null) { transferStream.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { //logout. Issues QUIT command ftpClient.logout(); } catch (IOException ioex) { ioex.printStackTrace(); } finally { try { //disconnect ftp session ftpClient.disconnect(); } catch (IOException ioexc) { ioexc.printStackTrace(); } } } } } log.debug("wapFTPZipFile FTP end. FileName: " + transferFile.getName() + "\t transfered: " + transfered); return transfered; }
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 w w . j a v a 2 s .c o 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.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 www . ja v a2 s . c o m*/ 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:de.ep3.ftpc.controller.portal.CrawlerDownloadController.java
@Override public void mouseClicked(MouseEvent e) { CrawlerResultsItem.PreviewPanel previewPanel = (CrawlerResultsItem.PreviewPanel) e.getSource(); CrawlerResult crawlerResult = previewPanel.getCrawlerResult(); CrawlerFile crawlerFile = crawlerResult.getFile(); FTPClient ftpClient = crawlerResult.getFtpClient(); if (ftpClient.isConnected()) { JOptionPane.showMessageDialog(portalFrame, i18n.translate("crawlerDownloadWhileConnected"), null, JOptionPane.ERROR_MESSAGE); return;/* w ww. jav a2 s .c o m*/ } String fileExtension = crawlerFile.getExtension(); JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter chooserFilter = new FileNameExtensionFilter( i18n.translate("fileType", fileExtension.toUpperCase()), crawlerFile.getExtension()); chooser.setApproveButtonText(i18n.translate("buttonSave")); chooser.setDialogTitle(i18n.translate("fileDownloadTo")); chooser.setDialogType(JFileChooser.SAVE_DIALOG); chooser.setFileFilter(chooserFilter); chooser.setSelectedFile(new File(crawlerFile.getName())); int selection = chooser.showSaveDialog(portalFrame); if (selection == JFileChooser.APPROVE_OPTION) { File fileToSave = chooser.getSelectedFile(); Server relatedServer = crawlerResult.getServer(); try { ftpClient.connect(relatedServer.need("server.ip"), Integer.parseInt(relatedServer.need("server.port"))); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { throw new IOException(i18n.translate("crawlerServerRefused")); } if (relatedServer.has("user.name")) { String userName = relatedServer.get("user.name"); String userPassword = ""; if (relatedServer.hasTemporary("user.password")) { userPassword = relatedServer.getTemporary("user.password"); } boolean loggedIn = ftpClient.login(userName, userPassword); if (!loggedIn) { throw new IOException(i18n.translate("crawlerServerAuthFail")); } } ftpClient.setFileType(FTP.BINARY_FILE_TYPE); /* Download file */ InputStream is = ftpClient.retrieveFileStream(crawlerFile.getFullName()); if (is != null) { byte[] rawFile = new byte[(int) crawlerFile.getSize()]; int i = 0; while (true) { int b = is.read(); if (b == -1) { break; } rawFile[i] = (byte) b; i++; /* Occasionally update the download progress */ if (i % 1024 == 0) { int progress = Math.round((((float) i) / crawlerFile.getSize()) * 100); status.add(i18n.translate("crawlerDownloadProgress", progress)); } } is.close(); is = null; if (!ftpClient.completePendingCommand()) { throw new IOException(); } Files.write(fileToSave.toPath(), rawFile); } /* Logout and disconnect */ ftpClient.logout(); tryDisconnect(ftpClient); status.add(i18n.translate("crawlerDownloadDone")); } catch (IOException ex) { tryDisconnect(ftpClient); JOptionPane.showMessageDialog(portalFrame, i18n.translate("crawlerDownloadFailed", ex.getMessage()), null, JOptionPane.ERROR_MESSAGE); } } }
From source file:jenkins.plugins.publish_over_ftp.jenkins.IntegrationTest.java
@Test public void testIntegration() throws Exception { final FTPClient mockFTPClient = mock(FTPClient.class); final int port = 21; final int timeout = 3000; final BapFtpHostConfiguration testHostConfig = new BapFtpHostConfiguration("testConfig", "testHostname", "testUsername", TEST_PASSWORD, "/testRemoteRoot", port, timeout, false, null, false, false) { @Override/* ww w .j av a 2 s. co m*/ public FTPClient createFTPClient() { return mockFTPClient; } }; new JenkinsTestHelper().setGlobalConfig(testHostConfig); final String dirToIgnore = "target"; final BapFtpTransfer transfer = new BapFtpTransfer("**/*", null, "sub-home", dirToIgnore, true, false, false, false, false, false, null); final ArrayList transfers = new ArrayList(Collections.singletonList(transfer)); final BapFtpPublisher publisher = new BapFtpPublisher(testHostConfig.getName(), false, transfers, false, false, null, null, null); final ArrayList publishers = new ArrayList(Collections.singletonList(publisher)); final BapFtpPublisherPlugin plugin = new BapFtpPublisherPlugin(publishers, false, false, false, "master", null); final FreeStyleProject project = createFreeStyleProject(); project.getPublishersList().add(plugin); final String buildDirectory = "build-dir"; final String buildFileName = "file.txt"; project.getBuildersList().add(new TestBuilder() { @Override public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener) throws InterruptedException, IOException { final FilePath dir = build.getWorkspace().child(dirToIgnore).child(buildDirectory); dir.mkdirs(); dir.child(buildFileName).write("Helloooooo", "UTF-8"); build.setResult(Result.SUCCESS); return true; } }); when(mockFTPClient.getReplyCode()).thenReturn(FTPReply.SERVICE_READY); when(mockFTPClient.login(testHostConfig.getUsername(), TEST_PASSWORD)).thenReturn(true); when(mockFTPClient.changeWorkingDirectory(testHostConfig.getRemoteRootDir())).thenReturn(true); when(mockFTPClient.setFileType(FTPClient.ASCII_FILE_TYPE)).thenReturn(true); when(mockFTPClient.changeWorkingDirectory(transfer.getRemoteDirectory())).thenReturn(false) .thenReturn(true); when(mockFTPClient.makeDirectory(transfer.getRemoteDirectory())).thenReturn(true); when(mockFTPClient.changeWorkingDirectory(buildDirectory)).thenReturn(false).thenReturn(true); when(mockFTPClient.makeDirectory(buildDirectory)).thenReturn(true); when(mockFTPClient.storeFile(eq(buildFileName), (InputStream) anyObject())).thenReturn(true); assertBuildStatusSuccess(project.scheduleBuild2(0).get()); verify(mockFTPClient).connect(testHostConfig.getHostname(), testHostConfig.getPort()); verify(mockFTPClient).storeFile(eq(buildFileName), (InputStream) anyObject()); verify(mockFTPClient).setDefaultTimeout(testHostConfig.getTimeout()); verify(mockFTPClient).setDataTimeout(testHostConfig.getTimeout()); }
From source file:com.limegroup.gnutella.archive.ArchiveContribution.java
/** * /*from w w w . j a v a2 s . co m*/ * @throws UnknownHostException * If the hostname cannot be resolved. * * @throws SocketException * If the socket timeout could not be set. * * @throws FTPConnectionClosedException * If the connection is closed by the server. * * @throws LoginFailedException * If the login fails. * * @throws DirectoryChangeFailedException * If changing to the directory provided by the internet * archive fails. * * @throws CopyStreamException * If an I/O error occurs while in the middle of * transferring a file. * * @throws IOException * If an I/O error occurs while sending a command or * receiving a reply from the server * * @throws IllegalStateException * If the contribution object is not ready to upload * (no username, password, server, etc. set) * or if java's xml parser is configured badly */ public void upload() throws UnknownHostException, SocketException, FTPConnectionClosedException, LoginFailedException, DirectoryChangeFailedException, CopyStreamException, RefusedConnectionException, IOException { final int NUM_XML_FILES = 2; final String META_XML_SUFFIX = "_meta.xml"; final String FILES_XML_SUFFIX = "_files.xml"; final String username = getUsername(); final String password = getPassword(); if (getFtpServer() == null) { throw new IllegalStateException("ftp server not set"); } if (getFtpPath() == null) { throw new IllegalStateException("ftp path not set"); } if (username == null) { throw new IllegalStateException("username not set"); } if (password == null) { throw new IllegalStateException("password not set"); } // calculate total number of files and bytes final String metaXmlString = serializeDocument(getMetaDocument()); final String filesXmlString = serializeDocument(getFilesDocument()); final byte[] metaXmlBytes = metaXmlString.getBytes(); final byte[] filesXmlBytes = filesXmlString.getBytes(); final int metaXmlLength = metaXmlBytes.length; final int filesXmlLength = filesXmlBytes.length; final Collection files = getFiles(); final int totalFiles = NUM_XML_FILES + files.size(); final String[] fileNames = new String[totalFiles]; final long[] fileSizes = new long[totalFiles]; final String metaXmlName = getIdentifier() + META_XML_SUFFIX; fileNames[0] = metaXmlName; fileSizes[0] = metaXmlLength; final String filesXmlName = getIdentifier() + FILES_XML_SUFFIX; fileNames[1] = filesXmlName; fileSizes[1] = filesXmlLength; int j = 2; for (Iterator i = files.iterator(); i.hasNext();) { final File f = (File) i.next(); fileNames[j] = f.getRemoteFileName(); fileSizes[j] = f.getFileSize(); j++; } // init the progress mapping for (int i = 0; i < fileSizes.length; i++) { _fileNames2Progress.put(fileNames[i], new UploadFileProgress(fileSizes[i])); _totalUploadSize += fileSizes[i]; } FTPClient ftp = new FTPClient(); try { // first connect if (isCancelled()) { return; } ftp.enterLocalPassiveMode(); if (isCancelled()) { return; } ftp.connect(getFtpServer()); final int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { throw new RefusedConnectionException(getFtpServer() + "refused FTP connection"); } // now login if (isCancelled()) { return; } if (!ftp.login(username, password)) { throw new LoginFailedException(); } try { // try to change the directory if (!ftp.changeWorkingDirectory(getFtpPath())) { // if changing fails, make the directory if (!isFtpDirPreMade() && !ftp.makeDirectory(getFtpPath())) { throw new DirectoryChangeFailedException(); } // now change directory, if it fails again bail if (isCancelled()) { return; } if (!ftp.changeWorkingDirectory(getFtpPath())) { throw new DirectoryChangeFailedException(); } } if (isCancelled()) { return; } connected(); // upload xml files uploadFile(metaXmlName, new ByteArrayInputStream(metaXmlBytes), ftp); uploadFile(filesXmlName, new ByteArrayInputStream(filesXmlBytes), ftp); // now switch to binary mode if (isCancelled()) { return; } ftp.setFileType(FTP.BINARY_FILE_TYPE); // upload contributed files for (final Iterator i = files.iterator(); i.hasNext();) { final File f = (File) i.next(); uploadFile(f.getRemoteFileName(), new FileInputStream(f.getIOFile()), ftp); } } catch (InterruptedIOException ioe) { // we've been requested to cancel return; } finally { ftp.logout(); // we don't care if logging out fails } } finally { try { ftp.disconnect(); } catch (IOException e) { } // don't care if disconnecting fails } // now tell the Internet Archive that we're done if (isCancelled()) { return; } checkinStarted(); if (isCancelled()) { return; } checkin(); if (isCancelled()) { return; } checkinCompleted(); }
From source file:de.ipk_gatersleben.ag_nw.graffiti.services.GUIhelper.java
private static ArrayList<String> listFiles(final BackgroundTaskStatusProviderSupportingExternalCall status, String downloadURL, String server, String remotePath, final FTPClient ftp) { String username;/*from w ww .j av a 2 s. c o m*/ String password; username = "anonymous@" + server; password = "anonymous"; final ObjectRef myoutputstream = new ObjectRef(); ftp.addProtocolCommandListener(new ProtocolCommandListener() { public void protocolCommandSent(ProtocolCommandEvent arg0) { status.setCurrentStatusText1("Command: " + arg0.getMessage()); } public void protocolReplyReceived(ProtocolCommandEvent arg0) { 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 LIST DIRECTORY: " + 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 new ArrayList<String>(); } if (!ftp.login("anonymous", "anonymous")) { if (!ftp.login(username, password)) { ftp.disconnect(); status.setCurrentStatusText1("Can't login to FTP server"); status.setCurrentStatusText2("ERROR"); return new ArrayList<String>(); } } 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.setRemoteVerificationEnabled(false); FTPFile[] res = ftp.listFiles(remotePath); ArrayList<String> result = new ArrayList<String>(); for (FTPFile r : res) { result.add(r.getName()); } return result; } catch (Exception err) { ErrorMsg.addErrorMessage(err); if (ftp != null && ftp.isConnected()) { try { System.out.println("Disconnect FTP connection (following error condition)"); ftp.disconnect(); } catch (Exception err2) { ErrorMsg.addErrorMessage(err2); } } return new ArrayList<String>(); } }
From source file:hydrograph.engine.spark.datasource.utils.FTPUtil.java
public void upload(RunFileTransferEntity runFileTransferEntity) { log.debug("Start FTPUtil upload"); FTPClient ftpClient = new FTPClient(); ftpClient.enterLocalPassiveMode();/*from ww w. ja va 2s .c o m*/ ftpClient.setBufferSize(1024000); int retryAttempt = runFileTransferEntity.getRetryAttempt(); int attemptCount = 1; int i = 0; InputStream inputStream = null; boolean login = false; File filecheck = new File(runFileTransferEntity.getInputFilePath()); log.info("input file name" + filecheck.getName()); if (runFileTransferEntity.getFailOnError()) { if (!(filecheck.isFile() || filecheck.isDirectory()) && !(runFileTransferEntity.getInputFilePath().contains("hdfs://"))) { log.error("Invalid input file path. Please provide valid input file path."); throw new FTPUtilException("Invalid input file path"); } } boolean done = false; for (i = 0; i < retryAttempt; i++) { try { log.info("Connection attempt: " + (i + 1)); if (runFileTransferEntity.getTimeOut() != 0) if (runFileTransferEntity.getEncoding() != null) ftpClient.setControlEncoding(runFileTransferEntity.getEncoding()); 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(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); if (runFileTransferEntity.getInputFilePath().contains("hdfs://")) { log.debug("Processing for HDFS input file path"); String inputPath = runFileTransferEntity.getInputFilePath(); String s1 = inputPath.substring(7, inputPath.length()); String s2 = s1.substring(0, s1.indexOf("/")); int index = runFileTransferEntity.getInputFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/").lastIndexOf('/'); String file_name = runFileTransferEntity.getInputFilePath().substring(index + 1); File f = new File("/tmp"); if (!f.exists()) f.mkdir(); Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://" + s2); FileSystem hdfsFileSystem = FileSystem.get(conf); Path local = new Path("/tmp"); String s = inputPath.substring(7, inputPath.length()); String hdfspath = s.substring(s.indexOf("/"), s.length()); File dir = new File(hdfspath); Random ran = new Random(); String tempFolder = "ftp_sftp_" + System.nanoTime() + "_" + ran.nextInt(1000); File dirs = new File("/tmp/" + tempFolder); boolean success = dirs.mkdirs(); if (hdfsFileSystem.isDirectory(new Path(hdfspath))) { log.debug("Provided HDFS input path is for directory."); InputStream is = null; OutputStream os = null; String localDirectory = hdfspath.substring(hdfspath.lastIndexOf("/") + 1); FileStatus[] fileStatus = hdfsFileSystem .listStatus(new Path(runFileTransferEntity.getInputFilePath())); Path[] paths = FileUtil.stat2Paths(fileStatus); try { String folderName = hdfspath.substring(hdfspath.lastIndexOf("/") + 1); Path hdfs = new Path(hdfspath); for (Path file : paths) { is = hdfsFileSystem.open(file); os = new BufferedOutputStream( new FileOutputStream(dirs + "" + File.separatorChar + file.getName())); IOUtils.copyBytes(is, os, conf); } ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); ftpClient.removeDirectory(folderName); ftpClient.makeDirectory(folderName); ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath().replaceAll( Matcher.quoteReplacement("\\"), "/") + File.separatorChar + folderName); for (File files : dirs.listFiles()) { if (files.isFile()) ftpClient.storeFile(files.getName().toString(), new BufferedInputStream(new FileInputStream(files))); } } catch (IOException e) { log.error("Failed while doing FTP file", e); //throw e; } finally { IOUtils.closeStream(is); IOUtils.closeStream(os); if (dirs != null) { FileUtils.deleteDirectory(dirs); } } } else { try { Path hdfs = new Path(hdfspath); hdfsFileSystem.copyToLocalFile(false, hdfs, local); inputStream = new FileInputStream(dirs + file_name); ftpClient.storeFile(file_name, new BufferedInputStream(inputStream)); } catch (Exception e) { log.error("Failed while doing FTP file", e); throw new FTPUtilException("Failed while doing FTP file", e); } finally { FileUtils.deleteDirectory(dirs); } } } else { java.nio.file.Path file = new File(runFileTransferEntity.getInputFilePath()).toPath(); if (Files.isDirectory(file)) { log.debug("Provided input file path is for directory"); File dir = new File(runFileTransferEntity.getInputFilePath()); String folderName = new File(runFileTransferEntity.getInputFilePath()).getName(); ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); try { ftpClient.removeDirectory(folderName); } catch (IOException e) { log.error("Failed while doing FTP file", e); throw new FTPUtilException("Failed while doing FTP file", e); } ftpClient.makeDirectory(folderName); ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/") + "/" + folderName); for (File files : dir.listFiles()) { if (files.isFile()) ftpClient.storeFile(files.getName().toString(), new BufferedInputStream(new FileInputStream(files))); } } else { inputStream = new FileInputStream(runFileTransferEntity.getInputFilePath()); ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); int index = runFileTransferEntity.getInputFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/").lastIndexOf('/'); String file_name = runFileTransferEntity.getInputFilePath().substring(index + 1); ftpClient.storeFile(file_name, new BufferedInputStream(inputStream)); } } } catch (Exception e) { log.error("Failed while doing FTP file", e); if (!login && runFileTransferEntity.getFailOnError()) { throw new FTPUtilException("Invalid FTP details"); } try { Thread.sleep(runFileTransferEntity.getRetryAfterDuration()); } catch (Exception e1) { log.error("Failed while sleeping for retry duration", e1); } continue; } finally { try { if (inputStream != null) inputStream.close(); } catch (IOException ioe) { } } done = true; break; } try { if (ftpClient != null) { ftpClient.logout(); ftpClient.disconnect(); } } catch (Exception e) { log.error("Failed while clossing the connection", e); } catch (Error e) { log.error("Failed while clossing the connection", e); //throw new RuntimeException(e); } if (runFileTransferEntity.getFailOnError() && !done) { log.error("File transfer failed"); throw new FTPUtilException("File transfer failed"); } else if (!done) { log.error("File transfer failed but mentioned fail on error as false"); } log.debug("Finished FTPUtil upload"); }
From source file:madkitgroupextension.export.Export.java
public static void updateFTP(FTPClient ftpClient, String _directory_dst, File _directory_src, File _current_file_transfert) throws IOException, TransfertException { ftpClient.changeWorkingDirectory("./"); FTPListParseEngine ftplpe = ftpClient.initiateListParsing(_directory_dst); FTPFile files[] = ftplpe.getFiles(); File current_file_transfert = _current_file_transfert; try {/*from w w w .j a v a 2 s . c o m*/ for (File f : _directory_src.listFiles()) { if (f.isDirectory()) { if (!f.getName().equals("./") && !f.getName().equals("../")) { if (_current_file_transfert != null) { if (!_current_file_transfert.getCanonicalPath().startsWith(f.getCanonicalPath())) continue; else _current_file_transfert = null; } boolean found = false; for (FTPFile ff : files) { if (f.getName().equals(ff.getName())) { if (ff.isFile()) { ftpClient.deleteFile(_directory_dst + ff.getName()); } else found = true; break; } } if (!found) { ftpClient.changeWorkingDirectory("./"); if (!ftpClient.makeDirectory(_directory_dst + f.getName() + "/")) System.err.println( "Impossible to create directory " + _directory_dst + f.getName() + "/"); } updateFTP(ftpClient, _directory_dst + f.getName() + "/", f, _current_file_transfert); } } else { if (_current_file_transfert != null) { if (!_current_file_transfert.equals(f.getCanonicalPath())) continue; else _current_file_transfert = null; } current_file_transfert = _current_file_transfert; FTPFile found = null; for (FTPFile ff : files) { if (f.getName().equals(ff.getName())) { if (ff.isDirectory()) { FileTools.removeDirectory(ftpClient, _directory_dst + ff.getName()); } else found = ff; break; } } if (found == null || (found.getTimestamp().getTimeInMillis() - f.lastModified()) < 0 || found.getSize() != f.length()) { FileInputStream fis = new FileInputStream(f); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); if (!ftpClient.storeFile(_directory_dst + f.getName(), fis)) System.err.println("Impossible to send file: " + _directory_dst + f.getName()); fis.close(); for (FTPFile ff : ftplpe.getFiles()) { if (f.getName().equals(ff.getName())) { f.setLastModified(ff.getTimestamp().getTimeInMillis()); break; } } } } } } catch (IOException e) { throw new TransfertException(current_file_transfert, null, e); } for (FTPFile ff : files) { if (!ff.getName().equals(".") && !ff.getName().equals("..")) { boolean found = false; for (File f : _directory_src.listFiles()) { if (f.getName().equals(ff.getName()) && f.isDirectory() == ff.isDirectory()) { found = true; break; } } if (!found) { if (ff.isDirectory()) { FileTools.removeDirectory(ftpClient, _directory_dst + ff.getName()); } else { ftpClient.deleteFile(_directory_dst + ff.getName()); } } } } }