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;
}
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;
}
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;
}
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);
}
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;
}
}
}
Aggregations