本文整理匯總了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;
}