Search in sources :

Example 76 with FTPFile

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);
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 77 with FTPFile

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);
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 78 with FTPFile

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();
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) Test(org.junit.Test)

Example 79 with FTPFile

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"));
}
Also used : MessagingException(org.springframework.messaging.MessagingException) SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) RunWith(org.junit.runner.RunWith) LiteralExpression(org.springframework.expression.common.LiteralExpression) Autowired(org.springframework.beans.factory.annotation.Autowired) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) SessionCallbackWithoutResult(org.springframework.integration.file.remote.SessionCallbackWithoutResult) Assert.fail(org.junit.Assert.fail) FTPClient(org.apache.commons.net.ftp.FTPClient) ClientCallbackWithoutResult(org.springframework.integration.file.remote.ClientCallbackWithoutResult) Assert.assertTrue(org.junit.Assert.assertTrue) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) FtpTestSupport(org.springframework.integration.ftp.FtpTestSupport) File(java.io.File) Configuration(org.springframework.context.annotation.Configuration) Assert.assertFalse(org.junit.Assert.assertFalse) ContextConfiguration(org.springframework.test.context.ContextConfiguration) FTPFile(org.apache.commons.net.ftp.FTPFile) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) LiteralExpression(org.springframework.expression.common.LiteralExpression) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) FTPClient(org.apache.commons.net.ftp.FTPClient) Test(org.junit.Test)

Example 80 with FTPFile

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;
}
Also used : CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) FTPFile(org.apache.commons.net.ftp.FTPFile) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) FtpPersistentAcceptOnceFileListFilter(org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter)

Aggregations

FTPFile (org.apache.commons.net.ftp.FTPFile)120 IOException (java.io.IOException)59 FTPClient (org.apache.commons.net.ftp.FTPClient)34 Test (org.junit.Test)32 File (java.io.File)28 InputStream (java.io.InputStream)16 ArrayList (java.util.ArrayList)15 FrameworkException (org.structr.common.error.FrameworkException)15 Tx (org.structr.core.graph.Tx)15 FtpTest (org.structr.web.files.FtpTest)15 FileOutputStream (java.io.FileOutputStream)11 OutputStream (java.io.OutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 BuildException (org.apache.tools.ant.BuildException)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 HashSet (java.util.HashSet)4