/** * @param ftpClient * @param allowFileTypes * @return */ public static void listFiles( FTPClient ftpClient, List<String> filePath, Set<String> allowFileTypes, String changeToPath, Boolean isFirst) { try { if (!isFirst) { Boolean flag = ftpClient.changeWorkingDirectory(changeToPath); if (!flag) { return; } } FTPFile[] files = ftpClient.listFiles(); if (null != files && files.length > 0) { for (FTPFile file : files) { String name = file.getName(); if (file.isDirectory()) { listFiles( ftpClient, filePath, allowFileTypes, changeToPath + "/" + name, Boolean.FALSE); } else if (file.isFile()) { String extAttr = getExt(name); if (allowFileTypes.contains(extAttr)) { filePath.add(getAfterUploadUrl(changeToPath + "/", name)); } } } } } catch (IOException e) { e.printStackTrace(); } }
/** @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile() */ @Override public void testParseFieldsOnFile() throws Exception { FTPFile file = getParser().parseFTPEntry("5000000000 A 11-17-98 16:07 POPUPLOG.OS2"); assertNotNull("Could not parse entry.", file); assertTrue("Should have been a file.", file.isFile()); assertEquals(5000000000L, file.getSize()); assertEquals("POPUPLOG.OS2", file.getName()); assertEquals("Tue Nov 17 16:07:00 1998", df.format(file.getTimestamp().getTime())); }
/** * check if a file exists or not * * @return FTPCLient * @throws IOException * @throws PageException */ private AFTPClient actionExistsFile() throws PageException, IOException { required("remotefile", remotefile); AFTPClient client = getClient(); FTPFile file = existsFile(client, remotefile, true); Struct cfftp = writeCfftp(client); cfftp.setEL(RETURN_VALUE, Caster.toBoolean(file != null && file.isFile())); cfftp.setEL(SUCCEEDED, Boolean.TRUE); stoponerror = false; return client; }
protected boolean doPollDirectory( String absolutePath, String dirName, List<GenericFile<FTPFile>> fileList, int depth) { log.trace("doPollDirectory from absolutePath: {}, dirName: {}", absolutePath, dirName); depth++; // remove trailing / dirName = FileUtil.stripTrailingSeparator(dirName); // compute dir depending on stepwise is enabled or not String dir; if (isStepwise()) { dir = ObjectHelper.isNotEmpty(dirName) ? dirName : absolutePath; operations.changeCurrentDirectory(dir); } else { dir = absolutePath; } log.trace("Polling directory: {}", dir); List<FTPFile> files = null; if (isUseList()) { if (isStepwise()) { files = operations.listFiles(); } else { files = operations.listFiles(dir); } } else { // we cannot use the LIST command(s) so we can only poll a named file // so created a pseudo file with that name FTPFile file = new FTPFile(); file.setType(FTPFile.FILE_TYPE); fileExpressionResult = evaluateFileExpression(); if (fileExpressionResult != null) { file.setName(fileExpressionResult); files = new ArrayList<FTPFile>(1); files.add(file); } } if (files == null || files.isEmpty()) { // no files in this directory to poll log.trace("No files found in directory: {}", dir); return true; } else { // we found some files log.trace("Found {} in directory: {}", files.size(), dir); } for (FTPFile file : files) { if (log.isTraceEnabled()) { log.trace( "FtpFile[name={}, dir={}, file={}]", new Object[] {file.getName(), file.isDirectory(), file.isFile()}); } // check if we can continue polling in files if (!canPollMoreFiles(fileList)) { return false; } if (file.isDirectory()) { RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset()); if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && isValidFile(remote, true, files)) { // recursive scan and add the sub files and folders String subDirectory = file.getName(); String path = absolutePath + "/" + subDirectory; boolean canPollMore = pollSubDirectory(path, subDirectory, fileList, depth); if (!canPollMore) { return false; } } } else if (file.isFile()) { RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset()); if (depth >= endpoint.getMinDepth() && isValidFile(remote, false, files)) { // matched file so add fileList.add(remote); } } else { log.debug("Ignoring unsupported remote file type: " + file); } } return true; }
public boolean isFolder() { return !ftpFile.isFile(); }
@Override public Future<Set<File>> download(LayerRequest currentLayer) throws Exception { this.currentLayer = currentLayer; currentLayer.setMetadata(this.includesMetadata()); File directory = getDirectory(); Set<File> fileSet = new HashSet<File>(); List<String> urls = this.getUrls(currentLayer); for (String url : urls) { InputStream inputStream = null; URL currentURL = new URL(url); String ftpServerAddress = currentURL.getHost(); String currentPath = currentURL.getPath(); FTPClient ftp = new FTPClient(); int delimiterIndex = currentPath.lastIndexOf("/"); String remoteDirectory = currentPath.substring(0, delimiterIndex); String fileName = currentPath.substring(delimiterIndex + 1); try { int reply; ftp.connect(ftpServerAddress); // Although they are open FTP servers, in order to access the files, a anonymous login is // necessary. ftp.login("anonymous", "anonymous"); System.out.println("Connected to " + ftpServerAddress + "."); System.out.print(ftp.getReplyString()); // 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."); System.exit(1); } reply = ftp.getReplyCode(); // enter passive mode ftp.enterLocalPassiveMode(); // change current directory ftp.changeWorkingDirectory(remoteDirectory); // check if the file with given name exists in ftp server try { FTPFile[] ftpFiles = ftp.listFiles(fileName); if (ftpFiles != null && ftpFiles.length > 0) { for (FTPFile file : ftpFiles) { if (!file.isFile()) continue; System.out.println("Found file:" + file.getName()); // transfer the file inputStream = ftp.retrieveFileStream(file.getName()); // save the file and add to fileset // TODO: ftp file does not contain MIME type. File outputFile = OgpFileUtils.createNewFileFromDownload(fileName, "ZIP", directory); // FileUtils with a BufferedInputStream seems to be the fastest method with a small // sample size. requires more testing BufferedInputStream bufferedIn = null; try { bufferedIn = new BufferedInputStream(inputStream); FileUtils.copyInputStreamToFile(bufferedIn, outputFile); fileSet.add(outputFile); } finally { IOUtils.closeQuietly(bufferedIn); } } } } catch (IOException e) { e.printStackTrace(); } finally { ftp.logout(); } } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } IOUtils.closeQuietly(inputStream); } } return new AsyncResult<Set<File>>(fileSet); };
public boolean accept(@Nullable final FTPFile aFile) { return aFile != null && aFile.isFile() && aFile.getName().startsWith(m_sStart); }
@Override public boolean isRegularFile() { return attributes.isFile(); }