List of usage examples for org.apache.commons.net.ftp FTPFile getName
public String getName()
From source file:org.bigmouth.nvwa.network.ftp.client.SimpleFTPClient.java
@Override public boolean exists(String path, String fileName) throws IOException { try {/*w ww. j a va 2s . c o m*/ this.connect(); if (dirExists(path)) { FTPFile[] listFiles = client.listFiles(path); if (ArrayUtils.isNotEmpty(listFiles)) { for (FTPFile ftpFile : listFiles) { if (ftpFile.getName().equals(fileName)) return true; } } } if (LOGGER.isWarnEnabled()) LOGGER.warn("File " + path + fileName + " does not exist."); return false; } catch (IOException e) { LOGGER.error("exists: ", e); throw e; } finally { if (autoDisconnect) this.disconnect(); } }
From source file:org.codelibs.fess.crawler.client.ftp.FtpClient.java
protected ResponseData getResponseData(final String uri, final boolean includeContent) { final ResponseData responseData = new ResponseData(); FTPClient client = null;// w w w .j ava 2 s . c o m try { responseData.setMethod(Constants.GET_METHOD); final FtpInfo ftpInfo = new FtpInfo(uri); responseData.setUrl(ftpInfo.toUrl()); client = getClient(ftpInfo); FTPFile file = null; client.changeWorkingDirectory(ftpInfo.getParent()); validateRequest(client); if (ftpInfo.getName() == null) { // root directory final Set<RequestData> requestDataSet = new HashSet<>(); if (includeContent) { try { final FTPFile[] files = client.listFiles(ftpInfo.getParent(), FTPFileFilters.NON_NULL); validateRequest(client); for (final FTPFile f : files) { final String chileUri = ftpInfo.toUrl(f.getName()); requestDataSet.add(RequestDataBuilder.newRequestData().get().url(chileUri).build()); } } catch (final IOException e) { throw new CrawlingAccessException("Could not access " + uri, e); } } ftpClientQueue.offer(client); throw new ChildUrlsException(requestDataSet, this.getClass().getName() + "#getResponseData(String, boolean)"); } final FTPFile[] files = client.listFiles(null, FTPFileFilters.NON_NULL); validateRequest(client); for (final FTPFile f : files) { if (ftpInfo.getName().equals(f.getName())) { file = f; break; } } if (file == null) { responseData.setHttpStatusCode(Constants.NOT_FOUND_STATUS_CODE); responseData.setCharSet(charset); responseData.setContentLength(0); } else if (file.isFile()) { responseData.setHttpStatusCode(Constants.OK_STATUS_CODE); responseData.setCharSet(Constants.UTF_8); responseData.setLastModified(file.getTimestamp().getTime()); // check file size responseData.setContentLength(file.getSize()); checkMaxContentLength(responseData); if (includeContent) { File tempFile = null; File outputFile = null; try { tempFile = File.createTempFile("ftp-", ".tmp"); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile))) { if (!client.retrieveFile(ftpInfo.getName(), out)) { throw new CrawlingAccessException("Failed to retrieve: " + ftpInfo.toUrl()); } } final MimeTypeHelper mimeTypeHelper = crawlerContainer.getComponent("mimeTypeHelper"); try (InputStream is = new FileInputStream(tempFile)) { responseData.setMimeType(mimeTypeHelper.getContentType(is, file.getName())); } catch (final Exception e) { responseData.setMimeType(mimeTypeHelper.getContentType(null, file.getName())); } if (contentLengthHelper != null) { final long maxLength = contentLengthHelper.getMaxLength(responseData.getMimeType()); if (responseData.getContentLength() > maxLength) { throw new MaxLengthExceededException( "The content length (" + responseData.getContentLength() + " byte) is over " + maxLength + " byte. The url is " + uri); } } responseData.setCharSet(geCharSet(tempFile)); if (tempFile.length() < maxCachedContentSize) { try (InputStream contentStream = new BufferedInputStream( new FileInputStream(tempFile))) { responseData.setResponseBody(InputStreamUtil.getBytes(contentStream)); } } else { outputFile = File.createTempFile("crawler-FileSystemClient-", ".out"); CopyUtil.copy(tempFile, outputFile); responseData.setResponseBody(outputFile, true); } } catch (final Exception e) { logger.warn("I/O Exception.", e); responseData.setHttpStatusCode(Constants.SERVER_ERROR_STATUS_CODE); } finally { if (tempFile != null && !tempFile.delete()) { logger.warn("Could not delete " + tempFile.getAbsolutePath()); } if (outputFile != null && !outputFile.delete()) { logger.warn("Could not delete " + outputFile.getAbsolutePath()); } } } } else if (file.isDirectory()) { final Set<RequestData> requestDataSet = new HashSet<>(); if (includeContent) { try { final FTPFile[] ftpFiles = client.listFiles(ftpInfo.getName(), FTPFileFilters.NON_NULL); validateRequest(client); for (final FTPFile f : ftpFiles) { final String chileUri = ftpInfo.toUrl(f.getName()); requestDataSet.add(RequestDataBuilder.newRequestData().get().url(chileUri).build()); } } catch (final IOException e) { throw new CrawlingAccessException("Could not access " + uri, e); } } ftpClientQueue.offer(client); throw new ChildUrlsException(requestDataSet, this.getClass().getName() + "#getResponseData(String, boolean)"); } else { responseData.setHttpStatusCode(Constants.NOT_FOUND_STATUS_CODE); responseData.setCharSet(charset); responseData.setContentLength(0); } ftpClientQueue.offer(client); } catch (final CrawlerSystemException e) { IOUtils.closeQuietly(responseData); throw e; } catch (final Exception e) { IOUtils.closeQuietly(responseData); throw new CrawlingAccessException("Could not access " + uri, e); } return responseData; }
From source file:org.eclipse.datatools.connectivity.sample.ftp.internal.FtpContentProvider.java
private String getDirectory(FTPFileObject file) { StringBuffer sb = new StringBuffer(); FTPFileObject fileobj = file;//from w w w . j a va 2 s .com Object obj; FTPFile ftpFile; while (fileobj != null) { ftpFile = fileobj.getFTPFile(); sb.insert(0, ftpFile.getName()); sb.insert(0, "/"); obj = getParent(fileobj); if (obj instanceof FTPFileObject) { fileobj = (FTPFileObject) obj; } else { fileobj = null; } } return sb.toString(); }
From source file:org.eclipse.datatools.connectivity.sample.ftp.internal.FtpLabelProvider.java
public String getText(Object element) { String text;/* ww w .jav a2 s .c o m*/ if (element instanceof FTPFileObject) { FTPFile file = ((FTPFileObject) element).getFTPFile(); text = file.getName(); } else { text = super.getText(element); } return text; }
From source file:org.esa.nest.util.ftpUtils.java
public static long getFileSize(final FTPFile[] fileList, final String remoteFileName) { for (FTPFile file : fileList) { if (file.getName().equalsIgnoreCase(remoteFileName)) { return file.getSize(); }/*from w ww.j a v a 2 s .c o m*/ } return 0; }
From source file:org.esa.nest.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 v a 2s . com final Map<String, Long> fileSizeMap = new HashMap<String, Long>(900); if (useCachedListing) { org.jdom.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.esa.nest.util.ftpUtils.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 va 2 s . com for (FTPFile ftpFile : remoteFileList) { final Element fileElem = new Element(ftpFile.getName()); fileElem.setAttribute("size", String.valueOf(ftpFile.getSize())); remotePathElem.addContent(fileElem); } XMLSupport.SaveXML(doc, file.getAbsolutePath()); }
From source file:org.esa.snap.core.dataop.downloadable.FtpDownloader.java
public Map<String, Long> readRemoteFileListNoCache(final String remotePath) { final Map<String, Long> fileSizeMap = new HashMap<>(100); try {/*from w ww . j av a 2 s.c o m*/ final FTPFile[] remoteFileList = getRemoteFileList(remotePath); if (remoteFileList != null) { for (FTPFile ftpFile : remoteFileList) { fileSizeMap.put(ftpFile.getName(), ftpFile.getSize()); } } } catch (Exception e) { SystemUtils.LOG.warning("Unable to get remote file list " + e.getMessage()); } return fileSizeMap; }
From source file:org.esa.snap.core.dataop.downloadable.FtpDownloader.java
public static Map<String, Long> readRemoteFileList(final FtpDownloader ftp, final String server, final String remotePath) { boolean useCachedListing = true; final File listingFile = new File(SystemUtils.getCacheDir(), server + ".listing.xml"); if (!listingFile.exists()) useCachedListing = false;//from www . j a va2 s. c o 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<Content> 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<Content> 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(getFileName(fileElem.getName()), attrib.getLongValue()); } catch (Exception e) { // } } } } } if (!listingFound) useCachedListing = false; } } if (!useCachedListing) { try { final FTPFile[] remoteFileList = ftp.getRemoteFileList(remotePath); if (remoteFileList != null) { writeRemoteFileList(remoteFileList, server, remotePath, listingFile); for (FTPFile ftpFile : remoteFileList) { fileSizeMap.put(ftpFile.getName(), ftpFile.getSize()); } } } catch (Exception e) { SystemUtils.LOG.warning("Unable to get remote file list " + e.getMessage()); } } return fileSizeMap; }
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);//from ww w.j av a2 s .c o 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()); } }