Search in sources :

Example 1 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project hadoop by apache.

the class FTPFileSystem method listStatus.

/**
   * Convenience method, so that we don't open a new connection when using this
   * method from within another method. Otherwise every API invocation incurs
   * the overhead of opening/closing a TCP connection.
   */
private FileStatus[] listStatus(FTPClient client, Path file) throws IOException {
    Path workDir = new Path(client.printWorkingDirectory());
    Path absolute = makeAbsolute(workDir, file);
    FileStatus fileStat = getFileStatus(client, absolute);
    if (fileStat.isFile()) {
        return new FileStatus[] { fileStat };
    }
    FTPFile[] ftpFiles = client.listFiles(absolute.toUri().getPath());
    FileStatus[] fileStats = new FileStatus[ftpFiles.length];
    for (int i = 0; i < ftpFiles.length; i++) {
        fileStats[i] = getFileStatus(ftpFiles[i], absolute);
    }
    return fileStats;
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 2 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project camel by apache.

the class FtpComponent method buildFileEndpoint.

@Override
protected GenericFileEndpoint<FTPFile> buildFileEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    String baseUri = getBaseUri(uri);
    // lets make sure we create a new configuration as each endpoint can customize its own version
    // must pass on baseUri to the configuration (see above)
    FtpConfiguration config = new FtpConfiguration(new URI(baseUri));
    FtpUtils.ensureRelativeFtpDirectory(this, config);
    FtpEndpoint<FTPFile> answer = new FtpEndpoint<FTPFile>(uri, this, config);
    extractAndSetFtpClientConfigParameters(parameters, answer);
    extractAndSetFtpClientParameters(parameters, answer);
    return answer;
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) URI(java.net.URI)

Example 3 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project camel by apache.

the class FtpConsumer method isMatched.

@Override
protected boolean isMatched(GenericFile<FTPFile> file, String doneFileName, List<FTPFile> files) {
    String onlyName = FileUtil.stripPath(doneFileName);
    for (FTPFile f : files) {
        if (f.getName().equals(onlyName)) {
            return true;
        }
    }
    log.trace("Done file: {} does not exist", doneFileName);
    return false;
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 4 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project nifi by apache.

the class FTPTransfer method getRemoteFileInfo.

@Override
public FileInfo getRemoteFileInfo(final FlowFile flowFile, String path, String remoteFileName) throws IOException {
    final FTPClient client = getClient(flowFile);
    if (path == null) {
        int slashpos = remoteFileName.lastIndexOf('/');
        if (slashpos >= 0 && !remoteFileName.endsWith("/")) {
            path = remoteFileName.substring(0, slashpos);
            remoteFileName = remoteFileName.substring(slashpos + 1);
        } else {
            path = "";
        }
    }
    final FTPFile[] files = client.listFiles(path);
    FTPFile matchingFile = null;
    for (final FTPFile file : files) {
        if (file.getName().equalsIgnoreCase(remoteFileName)) {
            matchingFile = file;
            break;
        }
    }
    if (matchingFile == null) {
        return null;
    }
    return newFileInfo(matchingFile, path);
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 5 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project bamboobsc by billchen198318.

the class FtpClientUtils method get.

/**
	 * 把目錄下的檔案抓下來,如果有權限
	 * 
	 * @param cwdDirectory		cd 目錄
	 * @param storeDir			本地端目錄
	 * @param head				檔案開頭 代null或 空白 忽略
	 * @param deleteFtpFile		是否刪除 FTP 上檔案(取完後刪除)
	 * 
	 * @throws SocketException
	 * @throws IOException
	 * @throws Exception
	 */
public void get(String cwdDirectory, File storeDir, String head, boolean deleteFtpFile) throws SocketException, IOException, Exception {
    if (!this.ftpClient.isConnected()) {
        this.logger.error("FTP not connection...");
        throw new Exception("FTP not connection...");
    }
    // 非 binary mode 在類 vsFtpd 可能會有問題 
    this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
    if (cwdDirectory != null && !"".equals(cwdDirectory)) {
        this.ftpClient.cwd(cwdDirectory);
    }
    FTPFile[] ftpFiles = this.ftpClient.listFiles();
    for (int ix = 0; ftpFiles != null && ix < ftpFiles.length; ix++) {
        if (head != null && !"".equals(head)) {
            if (ftpFiles[ix].getName().indexOf(head) != 0) {
                logger.info("not get : " + ftpFiles[ix].getName());
                continue;
            }
        }
        logger.info(ftpFiles[ix]);
        if (ftpFiles[ix].isFile()) {
            File downloadFile = new File(storeDir.getPath() + "/" + ftpFiles[ix].getName());
            FileOutputStream fos = new FileOutputStream(downloadFile);
            if (this.ftpClient.retrieveFile(ftpFiles[ix].getName(), fos)) {
                logger.info("ftp GET (save to) : " + storeDir.getPath() + "/" + ftpFiles[ix].getName());
                if (deleteFtpFile) {
                    this.delete(ftpFiles[ix].getName());
                }
            }
            downloadFile = null;
            fos.close();
            fos = null;
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) SocketException(java.net.SocketException) IOException(java.io.IOException)

Aggregations

FTPFile (org.apache.commons.net.ftp.FTPFile)112 IOException (java.io.IOException)55 Test (org.junit.Test)32 FTPClient (org.apache.commons.net.ftp.FTPClient)29 File (java.io.File)28 FrameworkException (org.structr.common.error.FrameworkException)15 Tx (org.structr.core.graph.Tx)15 FtpTest (org.structr.web.files.FtpTest)15 ArrayList (java.util.ArrayList)14 InputStream (java.io.InputStream)13 FileOutputStream (java.io.FileOutputStream)11 OutputStream (java.io.OutputStream)8 BuildException (org.apache.tools.ant.BuildException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 List (java.util.List)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 HashSet (java.util.HashSet)4