use of org.apache.commons.net.ftp.FTPFile in project neubbs by nuitcoder.
the class FtpUtil method remove.
/**
* 完全删除
* - 递归操作
* - 删除指定路径的子文件,及子目录
*
* @param path 删除路径
* @throws IOException IO 异常
*/
private static void remove(String path) throws IOException {
moveServerPath(path);
FTPFile[] files = ftpClient.listFiles();
if (files.length == 0) {
// there are no sub-files, to remove directory
ftpClient.removeDirectory(path);
return;
}
for (FTPFile ftpFile : ftpClient.listFiles(path)) {
if (ftpFile.isFile()) {
ftpClient.deleteFile(path + "/" + ftpFile.getName());
} else if (ftpFile.isDirectory()) {
// recursion
remove(path + "/" + ftpFile.getName() + "/");
}
}
// finally delete the initial directory
ftpClient.removeDirectory(path);
}
use of org.apache.commons.net.ftp.FTPFile in project spring-integration-samples by spring-projects.
the class FileTransferDeleteAfterSuccessDemo method main.
public static void main(String[] args) throws Exception {
LOGGER.info("\n=========================================================" + "\n " + "\n Welcome to Spring Integration! " + "\n " + "\n For more information please visit: " + "\n http://www.springsource.org/spring-integration " + "\n " + "\n=========================================================");
final AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/expression-advice-context.xml");
context.registerShutdownHook();
@SuppressWarnings("unchecked") SessionFactory<FTPFile> sessionFactory = context.getBean(SessionFactory.class);
SourcePollingChannelAdapter fileInbound = context.getBean(SourcePollingChannelAdapter.class);
@SuppressWarnings("unchecked") Session<FTPFile> session = mock(Session.class);
when(sessionFactory.getSession()).thenReturn(session);
fileInbound.start();
LOGGER.info("\n=========================================================" + "\n " + "\n This is the Expression Advice Sample - " + "\n " + "\n Press 'Enter' to terminate. " + "\n " + "\n Place a file in ${java.io.tmpdir}/adviceDemo ending " + "\n with .txt " + "\n The demo simulates a file transfer followed by the " + "\n Advice deleting the file; the result of the deletion " + "\n is logged. " + "\n " + "\n=========================================================");
System.in.read();
context.close();
System.exit(0);
}
use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.
the class FtpPersistentAcceptOnceFileListFilterTests method testRollback.
@Test
public void testRollback() throws Exception {
FtpPersistentAcceptOnceFileListFilter filter = new FtpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "rollback:");
FTPFile ftpFile1 = new FTPFile();
ftpFile1.setName("foo");
ftpFile1.setTimestamp(Calendar.getInstance());
FTPFile ftpFile2 = new FTPFile();
ftpFile2.setName("bar");
ftpFile2.setTimestamp(Calendar.getInstance());
FTPFile ftpFile3 = new FTPFile();
ftpFile3.setName("baz");
ftpFile3.setTimestamp(Calendar.getInstance());
FTPFile[] files = new FTPFile[] { ftpFile1, ftpFile2, ftpFile3 };
List<FTPFile> passed = filter.filterFiles(files);
assertTrue(Arrays.equals(files, passed.toArray()));
List<FTPFile> now = filter.filterFiles(files);
assertEquals(0, now.size());
filter.rollback(passed.get(1), passed);
now = filter.filterFiles(files);
assertEquals(2, now.size());
assertEquals("bar", now.get(0).getName());
assertEquals("baz", now.get(1).getName());
now = filter.filterFiles(files);
assertEquals(0, now.size());
filter.close();
}
use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.
the class FtpRemoteFileTemplateTests method testINT3412AppendStatRmdir.
@Test
public void testINT3412AppendStatRmdir() throws IOException {
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
fileNameGenerator.setExpression("'foobar.txt'");
template.setFileNameGenerator(fileNameGenerator);
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setUseTemporaryFileName(false);
template.execute(session -> {
session.mkdir("foo/");
return session.mkdir("foo/bar/");
});
template.append(new GenericMessage<String>("foo"));
template.append(new GenericMessage<String>("bar"));
assertTrue(template.exists("foo/foobar.txt"));
template.executeWithClient((ClientCallbackWithoutResult<FTPClient>) client -> {
try {
FTPFile[] files = client.listFiles("foo/foobar.txt");
assertEquals(6, files[0].getSize());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
template.execute((SessionCallbackWithoutResult<FTPFile>) session -> {
assertTrue(session.remove("foo/foobar.txt"));
assertTrue(session.rmdir("foo/bar/"));
FTPFile[] files = session.list("foo/");
assertEquals(0, files.length);
assertTrue(session.rmdir("foo/"));
});
assertFalse(template.getSession().exists("foo"));
}
use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.
the class FtpStreamingInboundChannelAdapterSpec method composeFilters.
@SuppressWarnings("unchecked")
private CompositeFileListFilter<FTPFile> composeFilters(FileListFilter<FTPFile> fileListFilter) {
CompositeFileListFilter<FTPFile> compositeFileListFilter = new CompositeFileListFilter<>();
compositeFileListFilter.addFilters(fileListFilter, new FtpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "ftpStreamingMessageSource"));
return compositeFileListFilter;
}
Aggregations