use of org.apache.commons.net.ftp.FTPFile in project hutool by looly.
the class Ftp method lsFiles.
/**
* 遍历某个目录下所有文件和目录,不会递归遍历
*
* @param path 目录,如果目录不存在,抛出异常
* @return 文件或目录列表
* @throws FtpException 路径不存在
* @throws IORuntimeException IO异常
*/
public FTPFile[] lsFiles(String path) throws FtpException, IORuntimeException {
String pwd = null;
if (StrUtil.isNotBlank(path)) {
pwd = pwd();
if (false == isDir(path)) {
throw new FtpException("Change dir to [{}] error, maybe path not exist!", path);
}
}
FTPFile[] ftpFiles;
try {
ftpFiles = this.client.listFiles();
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
// 回到原目录
cd(pwd);
}
return ftpFiles;
}
use of org.apache.commons.net.ftp.FTPFile in project hutool by looly.
the class Ftp method recursiveDownloadFolder.
/**
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
*
* @param sourcePath ftp服务器目录
* @param destDir 本地目录
*/
@Override
public void recursiveDownloadFolder(String sourcePath, File destDir) {
String fileName;
String srcFile;
File destFile;
for (FTPFile ftpFile : lsFiles(sourcePath, null)) {
fileName = ftpFile.getName();
srcFile = StrUtil.format("{}/{}", sourcePath, fileName);
destFile = FileUtil.file(destDir, fileName);
if (false == ftpFile.isDirectory()) {
// 本地不存在文件或者ftp上文件有修改则下载
if (false == FileUtil.exist(destFile) || (ftpFile.getTimestamp().getTimeInMillis() > destFile.lastModified())) {
download(srcFile, destFile);
}
} else {
// 服务端依旧是目录,继续递归
FileUtil.mkdir(destFile);
recursiveDownloadFolder(srcFile, destFile);
}
}
}
use of org.apache.commons.net.ftp.FTPFile in project alfresco-repository by Alfresco.
the class FTPServerTest method testCWD.
/**
* Test CWD for FTP server
*
* @throws Exception
*/
public void testCWD() throws Exception {
logger.debug("Start testCWD");
FTPClient ftp = connectClient();
try {
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
fail("FTP server refused connection.");
}
boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
assertTrue("admin login successful", login);
FTPFile[] files = ftp.listFiles();
reply = ftp.getReplyCode();
assertTrue(FTPReply.isPositiveCompletion(reply));
assertTrue(files.length == 1);
boolean foundAlfresco = false;
for (FTPFile file : files) {
logger.debug("file name=" + file.getName());
assertTrue(file.isDirectory());
if (file.getName().equalsIgnoreCase("Alfresco")) {
foundAlfresco = true;
}
}
assertTrue(foundAlfresco);
// Change to Alfresco Dir that we know exists
reply = ftp.cwd("/Alfresco");
assertTrue(FTPReply.isPositiveCompletion(reply));
// relative path with space char
reply = ftp.cwd("Data Dictionary");
assertTrue(FTPReply.isPositiveCompletion(reply));
// non existant absolute
reply = ftp.cwd("/Garbage");
assertTrue(FTPReply.isNegativePermanent(reply));
reply = ftp.cwd("/Alfresco/User Homes");
assertTrue(FTPReply.isPositiveCompletion(reply));
// Wild card
reply = ftp.cwd("/Alfresco/User*Homes");
assertTrue("unable to change to /Alfresco User*Homes/", FTPReply.isPositiveCompletion(reply));
// // Single char pattern match
// reply = ftp.cwd("/Alfre?co");
// assertTrue("Unable to match single char /Alfre?co", FTPReply.isPositiveCompletion(reply));
// two level folder
reply = ftp.cwd("/Alfresco/Data Dictionary");
assertTrue("unable to change to /Alfresco/Data Dictionary", FTPReply.isPositiveCompletion(reply));
// go up one
reply = ftp.cwd("..");
assertTrue("unable to change to ..", FTPReply.isPositiveCompletion(reply));
reply = ftp.pwd();
ftp.getStatus();
assertTrue("unable to get status", FTPReply.isPositiveCompletion(reply));
// check we are at the correct point in the tree
reply = ftp.cwd("Data Dictionary");
assertTrue(FTPReply.isPositiveCompletion(reply));
} finally {
ftp.disconnect();
}
}
use of org.apache.commons.net.ftp.FTPFile in project alfresco-repository by Alfresco.
the class FTPServerTest method testCRUD.
/**
* Test CRUD for FTP server
*
* @throws Exception
*/
public void testCRUD() throws Exception {
final String PATH1 = "FTPServerTest";
final String PATH2 = "Second part";
logger.debug("Start testFTPCRUD");
FTPClient ftp = connectClient();
try {
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
fail("FTP server refused connection.");
}
boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
assertTrue("admin login successful", login);
reply = ftp.cwd("/Alfresco/User Homes");
assertTrue(FTPReply.isPositiveCompletion(reply));
// Delete the root directory in case it was left over from a previous test run
try {
ftp.removeDirectory(PATH1);
} catch (IOException e) {
// ignore this error
}
// make root directory
ftp.makeDirectory(PATH1);
ftp.cwd(PATH1);
// make sub-directory in new directory
ftp.makeDirectory(PATH2);
ftp.cwd(PATH2);
// List the files in the new directory
FTPFile[] files = ftp.listFiles();
assertTrue("files not empty", files.length == 0);
// Create a file
String FILE1_CONTENT_1 = "test file 1 content";
String FILE1_NAME = "testFile1.txt";
ftp.appendFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_1.getBytes("UTF-8")));
// Get the new file
FTPFile[] files2 = ftp.listFiles();
assertTrue("files not one", files2.length == 1);
InputStream is = ftp.retrieveFileStream(FILE1_NAME);
String content = inputStreamToString(is);
assertEquals("Content is not as expected", content, FILE1_CONTENT_1);
ftp.completePendingCommand();
// Update the file contents
String FILE1_CONTENT_2 = "That's how it is says Pooh!";
ftp.storeFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8")));
InputStream is2 = ftp.retrieveFileStream(FILE1_NAME);
String content2 = inputStreamToString(is2);
assertEquals("Content is not as expected", FILE1_CONTENT_2, content2);
ftp.completePendingCommand();
// now delete the file we have been using.
assertTrue(ftp.deleteFile(FILE1_NAME));
// negative test - file should have gone now.
assertFalse(ftp.deleteFile(FILE1_NAME));
} finally {
// clean up tree if left over from previous run
ftp.disconnect();
}
}
use of org.apache.commons.net.ftp.FTPFile in project alfresco-repository by Alfresco.
the class FTPServerTest method testModificationTime.
/**
* Test Setting the modification time FTP server
*
* @throws Exception
*/
public void testModificationTime() throws Exception {
final String PATH1 = "FTPServerTest";
final String PATH2 = "ModificationTime";
logger.debug("Start testModificationTime");
FTPClient ftp = connectClient();
try {
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
fail("FTP server refused connection.");
}
boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
assertTrue("admin login successful", login);
reply = ftp.cwd("/Alfresco/User Homes");
assertTrue(FTPReply.isPositiveCompletion(reply));
// Delete the root directory in case it was left over from a previous test run
try {
ftp.removeDirectory(PATH1);
} catch (IOException e) {
// ignore this error
}
// make root directory
ftp.makeDirectory(PATH1);
ftp.cwd(PATH1);
// make sub-directory in new directory
ftp.makeDirectory(PATH2);
ftp.cwd(PATH2);
// List the files in the new directory
FTPFile[] files = ftp.listFiles();
assertTrue("files not empty", files.length == 0);
// Create a file
String FILE1_CONTENT_1 = "test file 1 content";
String FILE1_NAME = "testFile1.txt";
ftp.appendFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_1.getBytes("UTF-8")));
String pathname = "/Alfresco/User Homes" + "/" + PATH1 + "/" + PATH2 + "/" + FILE1_NAME;
logger.debug("set modification time");
// YYYYMMDDhhmmss Time set to 2012 August 30 12:39:05
String olympicTime = "20120830123905";
ftp.setModificationTime(pathname, olympicTime);
String extractedTime = ftp.getModificationTime(pathname);
// Feature of the commons ftp library ExtractedTime has a "status code" first and is followed by newline chars
assertTrue("time not set correctly by explicit set time", extractedTime.contains(olympicTime));
// Get the new file
FTPFile[] files2 = ftp.listFiles();
assertTrue("files not one", files2.length == 1);
InputStream is = ftp.retrieveFileStream(FILE1_NAME);
String content = inputStreamToString(is);
assertEquals("Content is not as expected", content, FILE1_CONTENT_1);
ftp.completePendingCommand();
// Update the file contents without setting time directly
String FILE1_CONTENT_2 = "That's how it is says Pooh!";
ftp.storeFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8")));
InputStream is2 = ftp.retrieveFileStream(FILE1_NAME);
String content2 = inputStreamToString(is2);
assertEquals("Content is not as expected", FILE1_CONTENT_2, content2);
ftp.completePendingCommand();
extractedTime = ftp.getModificationTime(pathname);
assertFalse("time not moved on if time not explicitly set", extractedTime.contains(olympicTime));
// now delete the file we have been using.
assertTrue(ftp.deleteFile(FILE1_NAME));
// negative test - file should have gone now.
assertFalse(ftp.deleteFile(FILE1_NAME));
} finally {
// clean up tree if left over from previous run
ftp.disconnect();
}
}
Aggregations