List of usage examples for org.apache.commons.net.ftp FTPClient listFiles
public FTPFile[] listFiles() throws IOException
From source file:hd3gtv.storage.AbstractFileBridgeFtpNexio.java
public AbstractFile[] listFiles() { try {// w ww .ja va 2 s. c o m FTPClient ftpclient = connectMe(); FTPFile[] files = ftpclient.listFiles(); if (files == null) { return new AbstractFile[1]; } AbstractFile[] absfiles = new AbstractFile[files.length]; for (int pos = 0; pos < files.length; pos++) { if (files[pos].isDirectory() == false) { absfiles[pos] = new SimpleMediaFile(files[pos].getName(), this, files[pos].getSize()); } } ftpclient.disconnect(); return absfiles; } catch (IOException e) { Log2.log.error("Can't dirlist", e, this); } return null; }
From source file:main.TestManager.java
/** * Deletes all local tests, downloads tests from the server * and saves all downloaded test in the local directory. *///from ww w .j a v a2 s. c o m public static void syncTestsWithServer() { FTPClient ftp = new FTPClient(); boolean error = false; try { int reply; String server = "zajicek.endora.cz"; ftp.connect(server, 21); // After connection attempt, we check the reply code to verify // success. reply = ftp.getReplyCode(); //Not connected successfully - inform the user if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); Debugger.println("FTP server refused connection."); ErrorInformer.failedSyncing(); return; } // LOGIN boolean success = ftp.login("plakato", "L13nK4"); Debugger.println("Login successful: " + success); if (success == false) { ftp.disconnect(); ErrorInformer.failedSyncing(); return; } ftp.enterLocalPassiveMode(); // Get all files from the server FTPFile[] files = ftp.listFiles(); System.out.println("Got files! Count: " + files.length); // Delete all current test to be replaced with // actulized version File[] locals = new File("src/tests/").listFiles(); for (File f : locals) { if (f.getName() == "." || f.getName() == "..") { continue; } f.delete(); } // Copy the files from server to local folder int failed = 0; for (FTPFile f : files) { if (f.isFile()) { Debugger.println(f.getName()); if (f.getName() == "." || f.getName() == "..") { continue; } File file = new File("src/tests/" + f.getName()); file.createNewFile(); OutputStream output = new FileOutputStream(file); if (!ftp.retrieveFile(f.getName(), output)) failed++; output.close(); } } // If we failed to download some file, inform the user if (failed != 0) { ftp.disconnect(); ErrorInformer.failedSyncing(); return; } // Disconnect ftp client or resolve the potential error ftp.logout(); } catch (IOException e) { error = true; e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } } if (error) { ErrorInformer.failedSyncing(); } Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("Download hotov"); alert.setHeaderText(null); alert.setContentText("Vetky testy boli zo servru spene stiahnut."); alert.showAndWait(); alert.close(); }
From source file:main.TestManager.java
/** * Deletes all files on the server and uploads all the * tests from the local directory./*from w w w. jav a2 s .c o m*/ */ public static void syncServerWithTest() { FTPClient ftp = new FTPClient(); boolean error = false; try { int reply; String server = "zajicek.endora.cz"; ftp.connect(server, 21); // After connection attempt, we check the reply code to verify // success. reply = ftp.getReplyCode(); //Not connected successfully - inform the user if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); Debugger.println("FTP server refused connection."); ErrorInformer.failedSyncing(); return; } // LOGIN boolean success = ftp.login("plakato", "L13nK4"); Debugger.println("Login successful: " + success); if (success == false) { ftp.disconnect(); ErrorInformer.failedSyncing(); return; } ftp.enterLocalPassiveMode(); // Get all files from the server FTPFile[] files = ftp.listFiles(); System.out.println("Got files! Count: " + files.length); // Delete all current tests on the server to be replaced with // actualized version from local directory for (FTPFile f : files) { if (f.getName() == "." || f.getName() == "..") { continue; } if (f.isFile()) { ftp.deleteFile(f.getName()); } } // Copy the files from local folder to server File localFolder = new File("src/tests/"); File[] localFiles = localFolder.listFiles(); int failed = 0; for (File f : localFiles) { if (f.getName() == "." || f.getName() == "..") { continue; } if (f.isFile()) { Debugger.println(f.getName()); File file = new File("src/tests/" + f.getName()); InputStream input = new FileInputStream(file); if (!ftp.storeFile(f.getName(), input)) failed++; input.close(); } } // If we failed to upload some file, inform the user if (failed != 0) { ftp.disconnect(); ErrorInformer.failedSyncing(); return; } // Disconnect ftp client or resolve the potential error ftp.logout(); } catch (IOException e) { error = true; e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } if (error) { ErrorInformer.failedSyncing(); return; } } Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("Upload hotov"); alert.setHeaderText(null); alert.setContentText("spene sa podarilo skoprova vetky testy na server!"); alert.showAndWait(); alert.close(); }
From source file:com.nostra13.universalimageloader.core.download.BaseImageDownloader.java
protected InputStream getStreamFromFTPNetwork(String imageUri, Object extra) throws IOException { int indexUrl = imageUri.indexOf("//") + 1; int indexUrlFinal = imageUri.lastIndexOf(":"); int slash = imageUri.lastIndexOf("/"); String ip = imageUri.substring(indexUrl + 1, indexUrlFinal); FTPClient client = new FTPClient(); client.connect(ip, 20000);/*from w w w . j a v a2s . co m*/ client.login("anonymous", ""); client.setFileType(FTP.BINARY_FILE_TYPE); client.enterLocalPassiveMode(); InputStream imageStream = null; FTPFile[] directories = client.listFiles(); long fileLength = (int) getFile(directories, imageUri.substring(slash + 1)).getSize(); try { imageStream = client.retrieveFileStream(imageUri.substring(slash + 1)); } catch (IOException e) { // Read all data to allow reuse connection (http://bit.ly/1ad35PY) IoUtils.readAndCloseStream(imageStream); throw e; } return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), fileLength); }
From source file:com.webarch.common.net.ftp.FtpService.java
/** * /*from ww w . ja v a 2 s .c om*/ * * @param remotePath ftp * @param fileName ??? * @param localPath ??? * @return true/false ? */ public boolean downloadFile(String remotePath, String fileName, String localPath) { boolean success = false; FTPClient ftpClient = new FTPClient(); try { int replyCode; ftpClient.connect(url, port); ftpClient.login(userName, password); replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { return false; } ftpClient.changeWorkingDirectory(remotePath); FTPFile[] files = ftpClient.listFiles(); for (FTPFile file : files) { if (file.getName().equals(fileName)) { File localFile = new File(localPath + File.separator + file.getName()); OutputStream outputStream = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), outputStream); outputStream.close(); } } ftpClient.logout(); success = true; } catch (IOException e) { logger.error("ftp?", e); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { logger.error("ftp?", e); } } } return success; }
From source file:com.bdaum.zoom.ui.internal.wizards.FtpDirPage.java
private ContainerCheckedTreeViewer createViewerGroup(Composite comp) { urlLabel = new Label(comp, SWT.NONE); urlLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); ExpandCollapseGroup expandCollapseGroup = new ExpandCollapseGroup(comp, SWT.NONE); final ContainerCheckedTreeViewer cbViewer = new ContainerCheckedTreeViewer(comp, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); expandCollapseGroup.setViewer(cbViewer); final Tree tree = cbViewer.getTree(); tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); cbViewer.setLabelProvider(new ZColumnLabelProvider() { @Override//ww w .j a va 2s .c om public String getText(Object element) { if (element instanceof FTPFile) return ((FTPFile) element).getName(); return element.toString(); } }); cbViewer.setContentProvider(new ITreeContentProvider() { public void inputChanged(Viewer v, Object oldInput, Object newInput) { fileParents.clear(); dirPaths.clear(); } public void dispose() { fileParents.clear(); dirPaths.clear(); } public Object[] getElements(Object inputElement) { if (inputElement instanceof FTPClient) { FTPClient ftpClient = (FTPClient) inputElement; List<FTPFile> files = new ArrayList<FTPFile>(); try { FTPFile[] listFiles = ftpClient.listFiles(); for (FTPFile ftpFile : listFiles) { if (ftpFile.isDirectory()) { if (!ftpFile.getName().endsWith(".")) { //$NON-NLS-1$ files.add(ftpFile); dirPaths.put(ftpFile, dir + '/' + ftpFile.getName()); } } else if (filter.accept(ftpFile.getName())) files.add(ftpFile); } } catch (IOException e) { // ignore } return files.toArray(); } return new Object[0]; } public boolean hasChildren(Object element) { if (element instanceof FTPFile) return ((FTPFile) element).isDirectory(); return false; } public Object getParent(Object element) { return fileParents.get(element); } public Object[] getChildren(Object parentElement) { if (parentElement instanceof FTPFile) { FTPFile parent = (FTPFile) parentElement; if (parent.isDirectory()) { String path = dirPaths.get(parent); if (path != null) try { if (ftp.changeWorkingDirectory(path)) { List<FTPFile> files = new ArrayList<FTPFile>(); for (FTPFile ftpFile : ftp.listFiles()) { if (ftpFile.isDirectory()) { if (!ftpFile.getName().endsWith(".")) { //$NON-NLS-1$ files.add(ftpFile); dirPaths.put(ftpFile, path + '/' + ftpFile.getName()); } } else if (filter.accept(ftpFile.getName())) files.add(ftpFile); fileParents.put(ftpFile, parent); } return files.toArray(); } } catch (IOException e) { // ignore } } } return new Object[0]; } }); cbViewer.setComparator(new ViewerComparator() { @Override public int compare(Viewer v, Object e1, Object e2) { if (e1 instanceof FTPFile && e2 instanceof FTPFile) { int i1 = ((FTPFile) e1).isDirectory() ? 1 : 2; int i2 = ((FTPFile) e2).isDirectory() ? 1 : 2; if (i1 != i2) return i1 - i2; return ((FTPFile) e1).getName().compareToIgnoreCase(((FTPFile) e2).getName()); } return super.compare(v, e1, e2); } }); cbViewer.addCheckStateListener(new ICheckStateListener() { public void checkStateChanged(CheckStateChangedEvent event) { validatePage(); } }); UiUtilities.installDoubleClickExpansion(cbViewer); return cbViewer; }
From source file:it.zero11.acme.example.FTPChallengeListener.java
private void deleteChallengeFiles() { FTPClient ftp = new FTPClient(); try {//from w w w.ja v a 2 s . c om ftp.connect(host); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { ftp.disconnect(); return; } ftp.login(username, password); ftp.changeWorkingDirectory(webroot); ftp.changeWorkingDirectory(".well-known"); ftp.changeWorkingDirectory("acme-challenge"); FTPFile[] subFiles = ftp.listFiles(); if (subFiles != null && subFiles.length > 0) { for (FTPFile aFile : subFiles) { String currentFileName = aFile.getName(); if (currentFileName.equals(".") || currentFileName.equals("..")) { continue; } else { ftp.deleteFile(currentFileName); } } } ftp.changeToParentDirectory(); ftp.removeDirectory("acme-challenge"); ftp.changeToParentDirectory(); ftp.removeDirectory(".well-known"); ftp.logout(); } catch (IOException e) { throw new AcmeException(e); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } }
From source file:com.dp2345.plugin.ftp.FtpPlugin.java
@Override public List<FileInfo> browser(String path) { List<FileInfo> fileInfos = new ArrayList<FileInfo>(); PluginConfig pluginConfig = getPluginConfig(); if (pluginConfig != null) { String host = pluginConfig.getAttribute("host"); Integer port = Integer.valueOf(pluginConfig.getAttribute("port")); String username = pluginConfig.getAttribute("username"); String password = pluginConfig.getAttribute("password"); String urlPrefix = pluginConfig.getAttribute("urlPrefix"); FTPClient ftpClient = new FTPClient(); try {/*from w w w. jav a 2 s .c o m*/ ftpClient.connect(host, port); ftpClient.login(username, password); ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()) && ftpClient.changeWorkingDirectory(path)) { for (FTPFile ftpFile : ftpClient.listFiles()) { FileInfo fileInfo = new FileInfo(); fileInfo.setName(ftpFile.getName()); fileInfo.setUrl(urlPrefix + path + ftpFile.getName()); fileInfo.setIsDirectory(ftpFile.isDirectory()); fileInfo.setSize(ftpFile.getSize()); fileInfo.setLastModified(ftpFile.getTimestamp().getTime()); fileInfos.add(fileInfo); } } } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { } } } } return fileInfos; }
From source file:com.hibo.bas.fileplugin.file.FtpPlugin.java
@Override public List<FileInfo> browser(String path) { List<FileInfo> fileInfos = new ArrayList<FileInfo>(); // PluginConfig pluginConfig = getPluginConfig(); // if (pluginConfig != null) { Map<String, String> ftpInfo = getFtpInfo("all"); String urlPrefix = DataConfig.getConfig("IMGFTPROOT"); FTPClient ftpClient = new FTPClient(); try {// w w w. j a va 2 s. c o m ftpClient.connect(ftpInfo.get("host"), 21); ftpClient.login(ftpInfo.get("username"), ftpInfo.get("password")); ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()) && ftpClient.changeWorkingDirectory(path)) { for (FTPFile ftpFile : ftpClient.listFiles()) { FileInfo fileInfo = new FileInfo(); fileInfo.setName(ftpFile.getName()); fileInfo.setUrl(urlPrefix + path + ftpFile.getName()); fileInfo.setIsDirectory(ftpFile.isDirectory()); fileInfo.setSize(ftpFile.getSize()); fileInfo.setLastModified(ftpFile.getTimestamp().getTime()); fileInfos.add(fileInfo); } } } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { } } } // } return fileInfos; }
From source file:com.claim.support.FtpUtil.java
public void uploadMutiFileWithFTP(String targetDirectory, FileTransferController fileTransferController) { try {// w ww.j ava2 s .co m FtpProperties properties = new ResourcesProperties().loadFTPProperties(); FTPClient ftp = new FTPClient(); ftp.connect(properties.getFtp_server()); if (!ftp.login(properties.getFtp_username(), properties.getFtp_password())) { ftp.logout(); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } ftp.enterLocalPassiveMode(); System.out.println("Remote system is " + ftp.getSystemName()); ftp.changeWorkingDirectory(targetDirectory); System.out.println("Current directory is " + ftp.printWorkingDirectory()); FTPFile[] ftpFiles = ftp.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { for (FTPFile file : ftpFiles) { if (!file.isFile()) { continue; } System.out.println("File is " + file.getName()); OutputStream output; output = new FileOutputStream( properties.getFtp_remote_directory() + File.separator + file.getName()); ftp.retrieveFile(file.getName(), output); output.close(); } } ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } }