List of usage examples for org.apache.commons.net.ftp FTPFile getName
public String getName()
From source file:org.esa.snap.util.ftpUtils.java
public static Map<String, Long> readRemoteFileList(final ftpUtils ftp, final String server, final String remotePath) { boolean useCachedListing = true; final String tmpDirUrl = ResourceUtils.getApplicationUserTempDataDir().getAbsolutePath(); final File listingFile = new File(tmpDirUrl + "//" + server + ".listing.xml"); if (!listingFile.exists()) useCachedListing = false;//from w w w .ja va 2s .co m final Map<String, Long> fileSizeMap = new HashMap<>(900); if (useCachedListing) { Document doc = null; try { doc = XMLSupport.LoadXML(listingFile.getAbsolutePath()); } catch (IOException e) { useCachedListing = false; } if (useCachedListing) { final Element root = doc.getRootElement(); boolean listingFound = false; final List children1 = root.getContent(); for (Object c1 : children1) { if (!(c1 instanceof Element)) continue; final Element remotePathElem = (Element) c1; final Attribute pathAttrib = remotePathElem.getAttribute("path"); if (pathAttrib != null && pathAttrib.getValue().equalsIgnoreCase(remotePath)) { listingFound = true; final List children2 = remotePathElem.getContent(); for (Object c2 : children2) { if (!(c2 instanceof Element)) continue; final Element fileElem = (Element) c2; final Attribute attrib = fileElem.getAttribute("size"); if (attrib != null) { try { fileSizeMap.put(fileElem.getName(), attrib.getLongValue()); } catch (Exception e) { // } } } } } if (!listingFound) useCachedListing = false; } } if (!useCachedListing) { try { final FTPFile[] remoteFileList = ftp.getRemoteFileList(remotePath); writeRemoteFileList(remoteFileList, server, remotePath, listingFile); for (FTPFile ftpFile : remoteFileList) { fileSizeMap.put(ftpFile.getName(), ftpFile.getSize()); } } catch (Exception e) { System.out.println("Unable to get remote file list " + e.getMessage()); } } return fileSizeMap; }
From source file:org.fao.fenix.ftp.FTPTask.java
private void checkFTPDirectory(String datasetDirectoryName) { try {//from ww w . ja va 2 s. c om if (ftpConnect()) { System.out.println("=== amis-ftp === FTPTask: checkFTPDirectory: == INFO == Task executing ..."); //enter passive mode ftpClient.enterLocalPassiveMode(); //get system name System.out.println("=== amis-ftp === FTPTask: checkFTPDirectory: == INFO == Remote system is " + ftpClient.getSystemType()); //change current directory ftpClient.changeWorkingDirectory(remoteDirectory + File.separator + datasetDirectoryName); System.out.println("=== amis-ftp === FTPTask: checkFTPDirectory: == INFO == Current directory is " + ftpClient.printWorkingDirectory()); //get list of filenames FTPFile[] ftpFiles = ftpClient.listFiles(); // Check the directory if (ftpFiles != null && ftpFiles.length > 0) { ArrayList csvFiles = new ArrayList(); for (FTPFile file : ftpFiles) { if (file.isFile() && file.getName().endsWith(".csv")) { csvFiles.add(file); } } FTPFile[] csvFTPFiles = (FTPFile[]) csvFiles.toArray(new FTPFile[csvFiles.size()]); System.out.println( "=== amis-ftp === FTPTask: checkFTPDirectory: == INFO == Current directory contains " + csvFTPFiles.length + " CSV File(s)"); if (csvFTPFiles.length == 1) { validateAndImportFile(csvFTPFiles, datasetDirectoryName); } else { System.out.println( "=== amis-ftp === FTPTask: checkFTPDirectory: == ERROR == The ftp://ext-ftp.fao.org" + ftpClient.printWorkingDirectory() + " directory contains " + ftpFiles.length + " files, instead of the expected 1. Please remove the redundant files. [THE ERROR EMAIL HAS BEEN SENT TO USER]"); //throw new FenixException("** The ftp://ext-ftp.fao.org"+ftpClient.printWorkingDirectory()+" directory contains "+ftpFiles.length+" files, instead of the expected 1. Please remove the redundant files."); } } else { System.out.println( "=== amis-ftp === FTPTask: checkFTPDirectory: == WARNING == The ftp://ext-ftp.fao.org" + ftpClient.printWorkingDirectory() + " directory contains no files. No action taken until 1 CSV file is there."); } ftpClient.logout(); ftpClient.disconnect(); } else { ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } //Email to Developers catch (FenixSystemException fse) { System.out.println( "=== amis-ftp === FTPTask: checkFTPDirectory: == ERROR == EMAIL SENT TO AMIS DEVELOPERS FenixSystemException: "); emailSender.sendEMailToDevelopers(emailErrorSubject, fse.getMessage()); //emailSender.sendEMailToContacts(emailErrorSubject, fse.getMessage(), false); } finally { try { if (jdbcConnector.getConn() != null) jdbcConnector.getConn().close(); if (ftpClient.isConnected()) ftpClient.disconnect(); } catch (SQLException se) { se.printStackTrace(); } catch (IOException se) { se.printStackTrace(); } } }
From source file:org.fao.fenix.ftp.FTPTask.java
private void validateAndImportFile(FTPFile[] ftpFiles, String datasetDirectoryName) { for (FTPFile file : ftpFiles) { // if (!file.isFile() && !file.getName().endsWith(".csv")) { // continue; // }/*from w w w. j a v a 2s . c om*/ Date fileDate = file.getTimestamp().getTime(); String formattedFileDate = df.format(fileDate); Date today = new java.util.Date(); String formattedTodayDate = df.format(today); if (formattedFileDate.equals(formattedTodayDate)) { String datasetCode = ""; String tempTableName = ""; if (datasetDirectoryName.equalsIgnoreCase("indices")) { datasetCode = "AMIS_IGC_DAILY_INDICATORS"; tempTableName = "igc_daily_indicators_temp"; } jdbcConnector.openConnection(); // Check the last modified date of the database String lastModifiedDate = amisDataImporter.getLastModifiedDate(datasetCode); System.out.println("=== amis-ftp === FTPTask: validateAndImportFile: == INFO == formattedFileDate " + formattedFileDate); System.out.println("=== amis-ftp === FTPTask: validateAndImportFile: == INFO == lastModifiedDate " + lastModifiedDate); if (!formattedFileDate.equals(lastModifiedDate)) { //Retrieve the file from the FTP and place in download directory retrieveFileFromFTP(formattedTodayDate, file); // connect to DB and get metadata for the dataset in order validate the csv Boolean isValidCSV = csvBasicValidator.validateCSV( getPath() + File.separator + formattedTodayDate + "_" + file.getName(), datasetCode); if (isValidCSV) { System.out.println( "=== amis-ftp === FTPTask: validateAndImportFile: == INFO == CSV is Validated ... proceed to import"); int expectedRows = csvBasicValidator.getLastLineNumber( getPath() + File.separator + formattedTodayDate + "_" + file.getName()); //set date format amisDataImporter.setPostgresDateFormat(); int rowsInsertedIntoTemp = amisDataImporter.importDataFromCSVIntoTempTable( formattedTodayDate + "_" + file.getName(), expectedRows, tempTableName); if (rowsInsertedIntoTemp == expectedRows) { //import the data into the final data table int rowsInserted = amisDataImporter.importDataFromCSVIntoDataTable( formattedTodayDate + "_" + file.getName(), datasetCode, expectedRows); System.out.println( "=== amis-ftp === FTPTask: validateAndImportFile: == INFO == Data Table: rowsInserted into " + datasetCode + " = " + rowsInserted + " (expected = " + expectedRows + ")"); //reset date format amisDataImporter.resetPostgresDateFormat(); int lastModifiedDateUpdatedRows = amisDataImporter.updateLastModifiedDate(datasetCode); System.out.println( "=== amis-ftp === FTPTask: validateAndImportFile: == INFO == CustomDataset Table: lastModifiedDateUpdated Executed affected " + lastModifiedDateUpdatedRows + " rows"); // Empty the download folder boolean isUploadedFileDeleted = emptyDownloadDirectory(); System.out.println( "=== amis-ftp === FTPTask: validateAndImportFile: == INFO == Is the uploaded file deleted from the 'download' directory: " + isUploadedFileDeleted); //Send Email once successfully updated! sendOnSuccessEmail(datasetCode); } else { //reset date format amisDataImporter.resetPostgresDateFormat(); System.out.println( "=== amis-ftp === FTPTask: validateAndImportFile: == ERROR == The number of rows in the CSV (" + expectedRows + ") do not match how many rows where inserted into the igc_daily_indicators_temp (temporary) database table (" + rowsInsertedIntoTemp + ")... so the import into the actual database table DID NOT happen "); String message = "FTPTask: validateAndImportFile(): The number of rows in the CSV (" + expectedRows + ") do not match how many rows where inserted into the igc_daily_indicators_temp (temporary) database table (" + rowsInsertedIntoTemp + ")... so the import into the actual database table DID NOT happen "; emailSender.sendEMailToDevelopers(emailErrorSubject, message); //emailSender.sendEMailToContacts(emailErrorSubject, message, false); } } csvBasicValidator.closeCSVParser(); } else { System.out.println( "=== amis-ftp === FTPTask: validateAndImportFile: == WARNING == The dataset was last modified on " + lastModifiedDate + ", which matches the date of the FTP file (i.e." + formattedFileDate + ") - so there is no need to re-upload"); } jdbcConnector.closeConnection(); } else { System.out.println( "=== amis-ftp === FTPTask: validateAndImportFile: == WARNING == No new files in the FTP for " + formattedTodayDate); } } }
From source file:org.fao.fenix.ftp.FTPTask.java
private void retrieveFileFromFTP(String formattedTodayDate, FTPFile ftpFile) throws FenixSystemException { try {//from w ww .j a v a 2 s . c o m OutputStream output = new FileOutputStream( getPath() + File.separator + formattedTodayDate + "_" + ftpFile.getName()); //get the file from the remote system ftpClient.retrieveFile(ftpFile.getName(), output); System.out.println( "=== amis-ftp === FTPTask: retrieveFileFromFTP: == INFO == File Name " + ftpFile.getName()); System.out.println("=== amis-ftp === FTPTask: retrieveFileFromFTP: == INFO == File downloaded " + getPath() + File.separator + formattedTodayDate + "_" + ftpFile.getName()); output.close(); } catch (FileNotFoundException fnfe) { System.out.println( "=== amis-ftp === FTPTask: retrieveFileFromFTP: == ERROR == Could not create file output stream (" + getPath() + File.separator + formattedTodayDate + "_" + ftpFile.getName() + ") = FileNotFoundException."); fnfe.printStackTrace(); throw new FenixSystemException("FTPTask: retrieveFileFromFTP(): Could not create file output stream (" + getPath() + File.separator + formattedTodayDate + "_" + ftpFile.getName() + ") = FileNotFoundException. --- " + fnfe.getMessage()); } catch (IOException ioe) { System.out.println( "=== amis-ftp === FTPTask: retrieveFileFromFTP: == ERROR == Could not retrieve FTP File and place in file output stream (" + getPath() + File.separator + formattedTodayDate + "_" + ftpFile.getName() + ") = IOException."); ioe.printStackTrace(); throw new FenixSystemException( "=== amis-ftp === FTPTask: Could not retrieve FTP File and place in file output stream (" + getPath() + File.separator + formattedTodayDate + "_" + ftpFile.getName() + ") = IOException. --- " + ioe.getMessage()); } }
From source file:org.giswater.util.UtilsFTP.java
private boolean checkMinorVersion(String currentMinorVersionFolder) { boolean updateMinorVersion = false; FTPFile[] listFolders = listDirectories(); FTPFile folder = listFolders[listFolders.length - 1]; newMinorVersionFolder = folder.getName().trim().toLowerCase(); newMinorVersion = Integer.parseInt(newMinorVersionFolder.substring(newMinorVersionFolder.length() - 1)); Utils.getLogger().info("FTP last minor version folder name: " + newMinorVersionFolder); if (!currentMinorVersionFolder.equals(newMinorVersionFolder)) { updateMinorVersion = true;//from w w w. ja v a 2s. c o m } return updateMinorVersion; }
From source file:org.giswater.util.UtilsFTP.java
private boolean checkBuildVersion(Integer majorVersion, Integer minorVersion, Integer buildVersion) { FTPFile[] listFolders = listDirectories(); if (listFolders.length == 0) return false; FTPFile folder = listFolders[listFolders.length - 1]; newBuildVersion = folder.getName(); ftpVersion = majorVersion + "." + newMinorVersion + "." + newBuildVersion; Utils.getLogger().info("FTP last version code: " + ftpVersion); Integer version = Integer.parseInt(folder.getName()); if (newMinorVersion > minorVersion) { return true; } else if (newMinorVersion == minorVersion && version > buildVersion) { return true; }/*w w w.j ava 2s .com*/ return false; }
From source file:org.ikasan.connector.ftp.net.FileTransferProtocolClient.java
private List<ClientListEntry> convertFTPFiles(String currentDir, FTPFile[] ftpFiles) throws URISyntaxException { List<ClientListEntry> list = new ArrayList<ClientListEntry>(ftpFiles.length); if (ftpFiles == null) { logger.debug("Directory was empty."); return list; }/*from ww w. j av a 2s. com*/ for (FTPFile ftpFile : ftpFiles) { // Apache net library can return null elements in list for // unparsed items if (ftpFile != null) { URI fileUri = this.getURI(currentDir, ftpFile.getName()); ClientListEntry entry = convertFTPFileToClientListEntry(ftpFile, fileUri, currentDir); list.add(entry); } else { logger.warn("One of the ftp file listings could not be parsed."); } } return list; }
From source file:org.ikasan.connector.ftp.net.FileTransferProtocolClient.java
/** * Constructing a <code>ClientListEntry</code> object from an * <code>FTPFile</code> object. This is a direct map with some formatting * changes./*from w w w . j a va 2 s. com*/ * * @param ftpFile The <code>FTPFile</code> to map to a * <code>ClientListEntry</code> * @param fileUri The URI of the underlying file for the particular * <code>FTPFile</code> * @return ClientListEntry */ private ClientListEntry convertFTPFileToClientListEntry(FTPFile ftpFile, URI fileUri, String currentDir) { ClientListEntry clientListEntry = new ClientListEntry(); clientListEntry.setUri(fileUri); clientListEntry.setName(ftpFile.getName()); clientListEntry.setFullPath(currentDir + System.getProperty("file.separator") + ftpFile.getName()); clientListEntry.setClientId(null); // Can't distinguish between Last Accessed and Last Modified clientListEntry.setDtLastAccessed(ftpFile.getTimestamp().getTime()); clientListEntry.setDtLastModified(ftpFile.getTimestamp().getTime()); clientListEntry.setSize(ftpFile.getSize()); clientListEntry.isDirectory(ftpFile.isDirectory()); clientListEntry.isLink(ftpFile.isSymbolicLink()); clientListEntry.setLongFilename(ftpFile.getRawListing()); clientListEntry.setAtime(ftpFile.getTimestamp().getTime().getTime()); clientListEntry.setMtime(ftpFile.getTimestamp().getTime().getTime()); clientListEntry.setAtimeString(ftpFile.getTimestamp().toString()); clientListEntry.setMtimeString(ftpFile.getTimestamp().toString()); // clientListEntry.setFlags(); clientListEntry.setGid(ftpFile.getGroup()); clientListEntry.setUid(ftpFile.getUser()); // TODO might be able to ask which permissions it has and build an int // and String from there // clientListEntry.setPermissions(); // clientListEntry.setPermissionsString(); // No extended information clientListEntry.setExtended(null); return clientListEntry; }
From source file:org.isatools.isacreator.filechooser.CustomFTPFile.java
/** * Compares two files by their name only, as opposed to complete path. * * @param file - other File to compare with. * @return Integer as a result of the compare operation. *///w w w . java 2s.co m public int compareTo(FTPFile file) { return ftp.getName().toLowerCase().compareTo(file.getName().toLowerCase()); }
From source file:org.isatools.isacreator.filechooser.FileChooserUI.java
/** * Create the Navigation Tree panel// ww w . j a v a 2s . c o m * * @return @see JPanel containing the navigation tree to browse a file system. */ private JPanel createNavTree() { JPanel treeContainer = new JPanel(new BorderLayout()); treeContainer.setBackground(UIHelper.BG_COLOR); treeContainer .setBorder(new TitledBorder(UIHelper.GREEN_ROUNDED_BORDER, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, UIHelper.VER_12_BOLD, UIHelper.DARK_GREEN_COLOR)); JPanel navigationControls = new JPanel(); navigationControls.setLayout(new BoxLayout(navigationControls, BoxLayout.LINE_AXIS)); navigationControls.setOpaque(false); final JLabel navToParentDir = new JLabel(upIcon); navToParentDir.setOpaque(false); navToParentDir.addMouseListener(new CommonMouseAdapter() { public void mousePressed(MouseEvent event) { super.mousePressed(event); navToParentDir.setIcon(upIcon); try { updateTree(fileBrowser.getParentDirectory()); } catch (IOException e) { errorAction("problem occurred!"); } } public void mouseEntered(MouseEvent event) { super.mouseEntered(event); navToParentDir.setIcon(upIconOver); } public void mouseExited(MouseEvent event) { super.mouseExited(event); navToParentDir.setIcon(upIcon); } }); navigationControls.add(navToParentDir); navigationControls.add(Box.createHorizontalStrut(5)); final JLabel navToHomeDir = new JLabel(homeIcon); navToHomeDir.setOpaque(false); navToHomeDir.addMouseListener(new CommonMouseAdapter() { public void mousePressed(MouseEvent event) { super.mousePressed(event); navToHomeDir.setIcon(homeIcon); try { updateTree(fileBrowser.getHomeDirectory()); } catch (IOException e) { if (e instanceof ConnectionException) { status.setText("<html>status: not connected!</html>"); } FileBrowserTreeNode defaultFTPNode = new FileBrowserTreeNode("problem occurred!", false, FileBrowserTreeNode.DIRECTORY); updateTree(defaultFTPNode); } } public void mouseEntered(MouseEvent event) { super.mouseEntered(event); navToHomeDir.setIcon(homeIconOver); } public void mouseExited(MouseEvent event) { super.mouseExited(event); navToHomeDir.setIcon(homeIcon); } }); navigationControls.add(navToHomeDir); navigationControls.add(Box.createGlue()); treeContainer.add(navigationControls, BorderLayout.NORTH); try { treeModel = new DefaultTreeModel(fileBrowser.getHomeDirectory()); directoryTree = new JTree(treeModel); directoryTree.setFont(UIHelper.VER_11_PLAIN); directoryTree.setCellRenderer(new FileSystemTreeCellRenderer()); } catch (IOException e) { FileBrowserTreeNode defaultFTPNode = new FileBrowserTreeNode("problem occurred!", false, FileBrowserTreeNode.DIRECTORY); updateTree(defaultFTPNode); } directoryTree.addMouseListener(new CommonMouseAdapter() { public void mousePressed(MouseEvent event) { super.mousePressed(event); int selRow = directoryTree.getRowForLocation(event.getX(), event.getY()); TreePath selPath = directoryTree.getPathForLocation(event.getX(), event.getY()); if (selRow != -1) { final FileBrowserTreeNode node = (FileBrowserTreeNode) selPath.getLastPathComponent(); if (SwingUtilities.isLeftMouseButton(event)) { if (event.getClickCount() == 2) { if ((node.getType() == FileBrowserTreeNode.DIRECTORY) && (node.getLevel() != 0)) { String newPath; if (fileBrowser instanceof LocalBrowser) { newPath = ((File) fileBrowser.getDirFiles().get(node.toString())).getPath(); } else { newPath = node.toString(); } updateTree(fileBrowser.changeDirectory(newPath)); } // else, if a leaf node, then add file to to list if (node.isLeaf() && (node.getType() != FileBrowserTreeNode.DIRECTORY)) { String extension = node.toString().substring(node.toString().lastIndexOf(".") + 1) .trim().toUpperCase(); FileChooserFile toAdd = null; for (Object o : fileBrowser.getFileMap().get(extension)) { String fileName; String filePath; if (fileBrowser instanceof LocalBrowser) { File file = (File) o; fileName = file.getName(); filePath = file.getPath(); if (fileName.equals(node.toString())) { toAdd = new CustomFile(filePath); break; } } else { FTPFile ftpFile = (FTPFile) o; fileName = ftpFile.getName(); filePath = fileBrowser.getAbsoluteWorkingDirectory() + File.separator + ftpFile.getName(); if (fileName.equals(node.toString())) { toAdd = new CustomFTPFile(ftpFile, filePath); break; } } } if (toAdd != null && !checkIfInList(toAdd)) { selectedFiles.addFileItem(toAdd); } } } } else { if ((node.getType() == FileBrowserTreeNode.DIRECTORY) && (node.getLevel() != 0)) { // show popup to add the directory to the selected files JPopupMenu popup = new JPopupMenu(); JMenuItem addDirectory = new JMenuItem("add directory"); addDirectory.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { Object fileToAdd = fileBrowser.getDirFiles().get(node.toString()); FileChooserFile toAdd; if (fileToAdd instanceof File) { toAdd = new CustomFile(((File) fileToAdd).getPath()); } else { FTPFile ftpFile = (FTPFile) fileToAdd; String filePath = fileBrowser.getAbsoluteWorkingDirectory() + File.separator + ftpFile.getName(); toAdd = new CustomFTPFile(ftpFile, filePath); } if (!checkIfInList(toAdd)) { selectedFiles.addDirectoryItem(toAdd); } } }); popup.add(addDirectory); popup.show(directoryTree, event.getX(), event.getY()); } } } } }); BasicTreeUI ui = new BasicTreeUI() { public Icon getCollapsedIcon() { return null; } public Icon getExpandedIcon() { return null; } }; directoryTree.setUI(ui); directoryTree.setFont(UIHelper.VER_12_PLAIN); JScrollPane treeScroll = new JScrollPane(directoryTree, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); treeScroll.setPreferredSize(new Dimension(300, 200)); treeScroll.setBorder(new EmptyBorder(0, 0, 0, 0)); treeContainer.add(treeScroll, BorderLayout.CENTER); IAppWidgetFactory.makeIAppScrollPane(treeScroll); return treeContainer; }