本文整理汇总了Java中org.apache.commons.net.ftp.FTPClient.listFiles方法的典型用法代码示例。如果您正苦于以下问题:Java FTPClient.listFiles方法的具体用法?Java FTPClient.listFiles怎么用?Java FTPClient.listFiles使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.net.ftp.FTPClient
的用法示例。
在下文中一共展示了FTPClient.listFiles方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: listStatus
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 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;
}
示例2: download
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
public static byte[] download(String url, int port, String username, String password, String remotePath,
String fileName) throws IOException {
FTPClient ftp = new FTPClient();
ftp.setConnectTimeout(5000);
ftp.setAutodetectUTF8(true);
ftp.setCharset(CharsetUtil.UTF_8);
ftp.setControlEncoding(CharsetUtil.UTF_8.name());
try {
ftp.connect(url, port);
ftp.login(username, password);// 登录
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
throw new IOException("login fail!");
}
ftp.changeWorkingDirectory(remotePath);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
try (ByteArrayOutputStream is = new ByteArrayOutputStream();) {
ftp.retrieveFile(ff.getName(), is);
byte[] result = is.toByteArray();
return result;
}
}
}
ftp.logout();
} finally {
if (ftp.isConnected()) {
ftp.disconnect();
}
}
return null;
}
示例3: getFileStatus
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 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 getFileStatus(FTPClient client, Path file)
throws IOException {
FileStatus fileStat = null;
Path workDir = new Path(client.printWorkingDirectory());
Path absolute = makeAbsolute(workDir, file);
Path parentPath = absolute.getParent();
if (parentPath == null) { // root dir
long length = -1; // Length of root dir on server not known
boolean isDir = true;
int blockReplication = 1;
long blockSize = DEFAULT_BLOCK_SIZE; // Block Size not known.
long modTime = -1; // Modification time of root dir not known.
Path root = new Path("/");
return new FileStatus(length, isDir, blockReplication, blockSize,
modTime, root.makeQualified(this));
}
String pathName = parentPath.toUri().getPath();
FTPFile[] ftpFiles = client.listFiles(pathName);
if (ftpFiles != null) {
for (FTPFile ftpFile : ftpFiles) {
if (ftpFile.getName().equals(file.getName())) { // file found in dir
fileStat = getFileStatus(ftpFile, parentPath);
break;
}
}
if (fileStat == null) {
throw new FileNotFoundException("File " + file + " does not exist.");
}
} else {
throw new FileNotFoundException("File " + file + " does not exist.");
}
return fileStat;
}
示例4: getFileList
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
public List<MetaFile2> getFileList() throws IOException, AuthenticationException {
FTPClient ftp = Session.getInstance().getFTPClient(mUri);
ftp.cwd(mUri.getPath());
org.apache.commons.net.ftp.FTPFile[] listFiles = ftp.listFiles();
if(listFiles==null)
return null;
ArrayList<MetaFile2> list = new ArrayList<MetaFile2>();
for(org.apache.commons.net.ftp.FTPFile f : listFiles){
if(!f.getName().equals("..")|| !f.getName().equals(".")){
FTPFile2 sf = new FTPFile2(f , Uri.withAppendedPath(mUri, f.getName()));
list.add(sf);
}
}
return list;
}
示例5: listSequentialDatasets
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
public static List<String> listSequentialDatasets(
String pdsName, Configuration conf) throws IOException {
List<String> datasets = new ArrayList<String>();
FTPClient ftp = null;
try {
ftp = getFTPConnection(conf);
if (ftp != null) {
ftp.changeWorkingDirectory("'" + pdsName + "'");
FTPFile[] ftpFiles = ftp.listFiles();
for (FTPFile f : ftpFiles) {
if (f.getType() == FTPFile.FILE_TYPE) {
datasets.add(f.getName());
}
}
}
} catch(IOException ioe) {
throw new IOException ("Could not list datasets from " + pdsName + ":"
+ ioe.toString());
} finally {
if (ftp != null) {
closeFTPConnection(ftp);
}
}
return datasets;
}
示例6: downloadFolderFiles
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 从远程服务器目录下载文件到本地服务器目录中
*
* @param ftp ftp对象
* @param localdir 本地文件夹路径
* @param remotedir 远程文件夹路径
* @param localTmpFile 本地下载文件名记录
* @return
* @throws IOException
*/
private Collection<String> downloadFolderFiles(FTPClient ftp, final String localdir, final String remotedir, final String localTmpFile) throws IOException {
//切换到下载目录的中
ftp.changeWorkingDirectory(remotedir);
//获取目录中所有的文件信息
FTPFile[] ftpfiles = ftp.listFiles();
Collection<String> fileNamesCol = new ArrayList<>();
//判断文件目录是否为空
if (!ArrayUtils.isEmpty(ftpfiles)) {
for (FTPFile ftpfile : ftpfiles) {
String remoteFilePath = ftpfile.getName();
String localFilePath = localdir + separator + ftpfile.getName();
//System.out.println("remoteFilePath ="+remoteFilePath +" localFilePath="+localFilePath)
//单个文件下载状态
DownloadStatus downStatus = FtpHelper.getInstance().download(ftp, remoteFilePath, localFilePath);
if (downStatus == DownloadStatus.Download_New_Success) {
//临时目录中添加记录信息
fileNamesCol.add(remoteFilePath);
}
}
}
if (!fileNamesCol.isEmpty() && !StringUtils.isEmpty(localTmpFile)) {
FileOperateUtils.writeLinesToFile(fileNamesCol, localTmpFile, false);
}
return fileNamesCol;
}
示例7: isFileExist
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 检查文件/文件夹下是否存在
*
* @param remoteDir 远程地址
* @param remoteFileName 远程文件名称
* @return true文件存在,false文件不存在
*/
public boolean isFileExist(String remoteDir, String remoteFileName) {
FTPClient ftp = null;
try {
ftp = initFtpClient(remoteDir);
if (ftp == null) {
logger.debug("ftp初始化失败");
return false;
}
FTPFile[] files = null;
files = ftp.listFiles(remoteDir + remoteFileName);
if (null != files && files.length > 0) {
return true;
}
return false;
} catch (IOException e) {
logger.error("FTP操作异常", e);
return false;
} finally {
close(ftp);
}
}
示例8: getFileSize
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 获取文件大小
*
* @param remoteDir 远程地址
* @param remoteFileName 远程文件名称
* @return 异常等情况返回0,其他情况返回正常
*/
public long getFileSize(String remoteDir, String remoteFileName) {
long fileSize = 0;
FTPClient ftp = null;
try {
ftp = initFtpClient(remoteDir);
if (ftp == null) {
logger.debug("ftp初始化失败");
return fileSize;
}
FTPFile[] files = null;
files = ftp.listFiles(remoteDir + remoteFileName);
if (null != files && files.length > 0) {
fileSize = files[0].getSize();
}
return fileSize;
} catch (IOException e) {
logger.error("FTP操作异常", e);
return fileSize;
} finally {
close(ftp);
}
}
示例9: GetDirAndFilesInfo
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 获取指定目录下的文件与目录信息集合
*
* @param currentDir 指定的当前目录
* @return 返回的文件集合
*/
public FTPFile[] GetDirAndFilesInfo(FTPClient ftpClient, String currentDir) {
FTPFile[] files = null;
try {
if (currentDir == null)
files = ftpClient.listFiles();
else
files = ftpClient.listFiles(currentDir);
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
//e.printStackTrace()
}
return files;
}
示例10: downloadFile
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* FTP下载
*
* @author wangwei
*/
public void downloadFile(String downloadFilename, String remoteFolder,
OutputStream outputStream, HttpServletResponse response)
throws Exception {
// 连接FTP服务器
FTPClient ftp = this.connectFTPServer();
try {
this.changeDirectory(remoteFolder);
FTPFile[] fs = ftp.listFiles();
for (int i = 0; i < fs.length; i++) {
FTPFile ff = fs[i];
if (ff.getName().equals(downloadFilename)) {
response.setHeader(
"Content-disposition",
"attachment;filename="
+ URLEncoder.encode(downloadFilename,
"utf-8"));
boolean result = ftp.retrieveFile(new String(ff.getName()
.getBytes("GBK"), "ISO-8859-1"), outputStream);
if (!result) {
throw new Exception("FTP文件下载失败!");
}
outputStream.flush();
}
}
} catch (Exception e) {
throw e;
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}
示例11: downloadFile
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
public static boolean downloadFile(String url, int port, String username, String password,
String remotePath, String fileName, String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);
FTPFile[] fs = ftp.listFiles();
System.out.println(fs.length);
for (FTPFile ff : fs) {
System.out.println(ff.getName());
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
示例12: testFTPConnectNegative
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Simple negative test that connects to the inbuilt ftp server and attempts to
* log on with the wrong password.
*
* @throws Exception
*/
public void testFTPConnectNegative() throws Exception
{
logger.debug("Start testFTPConnectNegative");
FTPClient ftp = connectClient();
try
{
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
fail("FTP server refused connection.");
}
boolean login = ftp.login(USER_ADMIN, "garbage");
assertFalse("admin login successful", login);
// now attempt to list the files and check that the command does not
// succeed
FTPFile[] files = ftp.listFiles();
assertNotNull(files);
assertTrue(files.length == 0);
reply = ftp.getReplyCode();
assertTrue(FTPReply.isNegativePermanent(reply));
}
finally
{
ftp.disconnect();
}
}
示例13: getRemoteFileInfo
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
@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);
}
示例14: deleteDir
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 删除文件夹下所有的文件(非文件夹)
*
* @param fullDir 待删除目录完整ftp路径
* @return 删除结果<br> true - 删除成功<br>
* false - 删除失败
*/
public boolean deleteDir(String fullDir) {
FTPClient ftp = null;
// fullDir = encode(fullDir);
try {
ftp = initFtpClient(fullDir);
if (ftp == null) {
logger.debug("ftp初始化失败");
return false;
}
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
for (FTPFile ftpFile : ftpFiles) {
if (!ftpFile.isDirectory()) {
boolean deleteRet = ftp.deleteFile(ftpFile.getName());
if (!deleteRet) {
logger.debug("删除文件失败");
return false;
}
}
}
}
return true;
} catch (IOException e) {
logger.error("FTP操作异常", e);
return false;
} finally {
close(ftp);
}
}
示例15: upload
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 上传文件到FTP服务器,支持断点续传
*
* @param localFile 本地文件
* @param remoteFilePath 远程文件路径,使用/home/directory1/subdirectory/file.ext
* 按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构
* @return 上传结果
* @throws IOException
*/
public UploadStatus upload(FTPClient ftpClient, File localFile, String remoteFilePath) throws IOException {
// 设置PassiveMode传输
ftpClient.enterLocalPassiveMode();
// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding(DEAFULT_REMOTE_CHARSET);
UploadStatus result;
// 对远程目录的处理
String remoteFileName = remoteFilePath;
if (remoteFilePath.contains("/")) {
remoteFileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
// 创建服务器远程目录结构,创建失败直接返回
if (createDirecroty(remoteFilePath, ftpClient) == UploadStatus.Create_Directory_Fail) {
return UploadStatus.Create_Directory_Fail;
}
}
// 检查远程是否存在文件
FTPFile[] files = ftpClient.listFiles(new String(remoteFileName
.getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET));
if (files.length == 1) {
long remoteSize = files[0].getSize();
// File f = new File(localFilePath)
long localSize = localFile.length();
if (remoteSize == localSize) { // 文件存在
return UploadStatus.File_Exits;
} else if (remoteSize > localSize) {
return UploadStatus.Remote_Bigger_Local;
}
// 尝试移动文件内读取指针,实现断点续传
result = uploadFile(remoteFileName, localFile, ftpClient, remoteSize);
// 如果断点续传没有成功,则删除服务器上文件,重新上传
if (result == UploadStatus.Upload_From_Break_Failed) {
if (!ftpClient.deleteFile(remoteFileName)) {
return UploadStatus.Delete_Remote_Faild;
}
result = uploadFile(remoteFileName, localFile, ftpClient, 0);
}
} else {
result = uploadFile(remoteFileName, localFile, ftpClient, 0);
}
return result;
}