List of usage examples for org.apache.commons.net.ftp FTPFile getSize
public long getSize()
From source file:org.esa.snap.core.dataop.downloadable.FtpDownloader.java
private static void writeRemoteFileList(final FTPFile[] remoteFileList, final String server, final String remotePath, final File file) { final Element root = new Element("remoteFileListing"); root.setAttribute("server", server); final Document doc = new Document(root); final Element remotePathElem = new Element("remotePath"); remotePathElem.setAttribute("path", remotePath); root.addContent(remotePathElem);// w w w. j a v a 2 s . co m for (FTPFile ftpFile : remoteFileList) { // add prefix just in case file name starts with a digit final Element fileElem = new Element(elemPrefix + ftpFile.getName()); fileElem.setAttribute("size", String.valueOf(ftpFile.getSize())); remotePathElem.addContent(fileElem); } try { XMLSupport.SaveXML(doc, file.getAbsolutePath()); } catch (IOException e) { SystemUtils.LOG.warning("Unable to save " + file.getAbsolutePath()); } }
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;/*ww w. ja v a 2 s . c om*/ 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.eurocarbdb.action.admin.AbstractDownloadAction.java
/************************************************ *//from www . j a v a 2s. c om * Downloads the named remote file to the given output stream. * This method blocks until download is complete, however * information about download progress may be obtained * asynchronously (in another thread) via the following methods: * <ul> * <li>getMillisecsElapsed</li> * <li>getPercentComplete</li> * <li>getDownloadSpeed</li> * </ul> * * @see FTP_Client#statFile */ protected void download(String filename, OutputStream out) throws IOException, FTPConnectionClosedException { if (ftpClient == null) throw new RuntimeException("client not yet connected!!!"); startTime = System.currentTimeMillis(); outStream = out; try { ftpClient.setPassiveMode(true); ftpClient.setBinaryMode(true); // work out file mtime and size in bytes FTPFile filestat = ftpClient.statFile(filename); // mjh:TODO check not null here this.fileSize = filestat.getSize(); this.fileTimestamp = filestat.getTimestamp(); if (log.isDebugEnabled()) { log.debug("File size is " + fileSize + ", last modified " + fileTimestamp); } // open input stream this.inStream = ftpClient.retrieveFileStream(filename); byte[] buffer = new byte[BUFFER_SIZE]; while (true) { // read as many bytes as are available, up to the length of the buffer. int available = inStream.available(); int bytes = (available <= buffer.length) ? available : buffer.length; // the call blocks until data is read int bytes_read = inStream.read(buffer, 0, bytes); if (bytes_read == -1 || bytesDownloaded == fileSize) break; if (bytes_read == 0) continue; //if ( log.isTraceEnabled() ) // log.trace("read " + bytes_read + " bytes"); bytesDownloaded += bytes_read; outStream.write(buffer, 0, bytes_read); } log.debug("finished transfer, ending FTP transaction"); try { inStream.close(); } catch (IOException ioe) { log.warn("Caught exception closing input streams:", ioe); } if (!ftpClient.completePendingCommand()) throw new IOException("completePendingCommand returned false, " + "probably indicating a failed download " + "(unspecified reason)"); log.info("download completed successfully"); } catch (FTPConnectionClosedException ioe) { log.warn("FTP server closed connection: ", ioe); ioe.printStackTrace(); closeConnection(); throw ioe; } catch (IOException ioe) { log.warn("Caught IO exception during FTP: ", ioe); ioe.printStackTrace(); closeConnection(); throw ioe; } }
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 www .ja va 2s .c o m * * @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.madeirahs.shared.provider.FTPProvider.java
/** * Note: accuracy of results for FTP servers are neither guaranteed nor * explicitly defined. The server may or may not provide the necessary file * size information and furthermore may update this information at its own * discretion.//from www . ja v a 2 s .c o m */ @Override public long sizeOf(String fileName) { try { guard.acquire(); FTPFile f = ftp.mlistFile(fileName); guard.release(); if (f == null) { return -1; } else { return f.getSize(); } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return -1; }
From source file:org.martin.ftp.util.RemoteComparator.java
@Override public int compare(FTPFile o1, FTPFile o2) { if (null != option) switch (option) { case DATE: if (order == SortOption.UPWARD) return (int) (o1.getTimestamp().getTimeInMillis() - o2.getTimestamp().getTimeInMillis()); else// ww w. j a va 2s . co m return (int) (o2.getTimestamp().getTimeInMillis() - o1.getTimestamp().getTimeInMillis()); case FORMAT: String file1Format = Utilities.getFormat(o1.getName()); String file2Format = Utilities.getFormat(o2.getName()); if (order == SortOption.UPWARD) if (o1.isDirectory() || o2.isDirectory()) { if (o1.isDirectory() && o2.isDirectory()) return idDirectory - idDirectory; else if (o1.isDirectory() && !o2.isDirectory()) return idDirectory - idFile; else return idFile - idDirectory; } else return file1Format.compareToIgnoreCase(file2Format); else if (o1.isDirectory() || o2.isDirectory()) { if (o1.isDirectory() && o2.isDirectory()) return idDirectory - idDirectory; else if (o1.isDirectory() && !o2.isDirectory()) return idFile - idDirectory; else return idDirectory - idFile; } else return file2Format.compareToIgnoreCase(file1Format); case NAME: if (order == SortOption.UPWARD) return o1.getName().compareToIgnoreCase(o2.getName()); else return o2.getName().compareToIgnoreCase(o1.getName()); case SIZE: if (order == SortOption.UPWARD) return (int) (o1.getSize() - o2.getSize()); else return (int) (o2.getSize() - o1.getSize()); case TYPE: if ((o1.isDirectory() && o2.isDirectory()) || (!o1.isDirectory() && !o2.isDirectory())) return idDirectory - idDirectory; if (order == SortOption.UPWARD) if (o1.isDirectory() && !o2.isDirectory()) return idDirectory - idFile; else return idFile - idDirectory; else if (o1.isDirectory() && !o2.isDirectory()) return idFile - idDirectory; else return idDirectory - idFile; default: return 0; } return 0; }
From source file:org.martin.ftp.util.SearchComparator.java
@Override public int compare(FileFiltering o1, FileFiltering o2) { FTPFile fileO1 = o1.getFile(); FTPFile fileO2 = o2.getFile();//from ww w.java2 s .c om if (null != option) switch (option) { case DATE: if (order == SortOption.UPWARD) return (int) (fileO1.getTimestamp().getTimeInMillis() - fileO2.getTimestamp().getTimeInMillis()); else return (int) (fileO2.getTimestamp().getTimeInMillis() - fileO1.getTimestamp().getTimeInMillis()); case FORMAT: String file1Format = Utilities.getFormat(fileO1.getName()); String file2Format = Utilities.getFormat(fileO2.getName()); if (order == SortOption.UPWARD) if (fileO1.isDirectory() || fileO2.isDirectory()) { if (fileO1.isDirectory() && fileO2.isDirectory()) return idDirectory - idDirectory; else if (fileO1.isDirectory() && !fileO2.isDirectory()) return idDirectory - idFile; else return idFile - idDirectory; } else return file1Format.compareToIgnoreCase(file2Format); else if (fileO1.isDirectory() || fileO2.isDirectory()) { if (fileO1.isDirectory() && fileO2.isDirectory()) return idDirectory - idDirectory; else if (fileO1.isDirectory() && !fileO2.isDirectory()) return idFile - idDirectory; else return idDirectory - idFile; } else return file2Format.compareToIgnoreCase(file1Format); case NAME: if (order == SortOption.UPWARD) return fileO1.getName().compareToIgnoreCase(fileO2.getName()); else return fileO2.getName().compareToIgnoreCase(fileO1.getName()); case SIZE: if (order == SortOption.UPWARD) return (int) (fileO1.getSize() - fileO2.getSize()); else return (int) (fileO2.getSize() - fileO1.getSize()); case TYPE: if ((fileO1.isDirectory() && fileO2.isDirectory()) || (!fileO1.isDirectory() && !fileO2.isDirectory())) return idDirectory - idDirectory; if (order == SortOption.UPWARD) if (fileO1.isDirectory() && !fileO2.isDirectory()) return idDirectory - idFile; else return idFile - idDirectory; else if (fileO1.isDirectory() && !fileO2.isDirectory()) return idFile - idDirectory; else return idDirectory - idFile; default: return 0; } return 0; }
From source file:org.mockftpserver.stub.StubFtpServerIntegrationTest.java
/** * Verify that the FTPFile has the specified properties * // ww w .j a va2 s.c om * @param ftpFile - the FTPFile to verify * @param type - the expected file type * @param name - the expected file name * @param size - the expected file size (will be zero for a directory) */ private void verifyFTPFile(FTPFile ftpFile, int type, String name, long size) { LOG.info(ftpFile); assertEquals("type: " + ftpFile, type, ftpFile.getType()); assertEquals("name: " + ftpFile, name, ftpFile.getName()); assertEquals("size: " + ftpFile, size, ftpFile.getSize()); }
From source file:org.mousephenotype.dcc.crawler.FtpCrawler.java
private ZipFile retrieveOrCreateZipFile(FilenameTokenizer t, FTPFile f) { DatabaseAccessor da = DatabaseAccessor.getInstance(); String filename = f.getName(); ZipFile z = da.getZipFile(filename); if (z == null) { FilenameTokens tokens = t.tokenize(filename); z = new ZipFile(filename); if (tokens != null) { z.setCentreId(tokens.getProducer()); z.setCreated(tokens.getCreated()); z.setInc(tokens.getInc());// www .j a v a 2s. c o m z.setSizeBytes(f.getSize()); } da.persist(z); } return z; }
From source file:org.mule.transport.ftp.FtpMuleMessageFactory.java
@Override protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception { super.addProperties(message, transportMessage); FTPFile file = (FTPFile) transportMessage; message.setInboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, file.getName()); message.setInboundProperty(FileConnector.PROPERTY_FILE_SIZE, file.getSize()); message.setInboundProperty(FileConnector.PROPERTY_FILE_TIMESTAMP, file.getTimestamp()); }