Search in sources :

Example 56 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project hutool by looly.

the class Ftp method init.

/**
 * 初始化连接
 *
 * @param config FTP配置
 * @param mode   模式
 * @return this
 */
public Ftp init(FtpConfig config, FtpMode mode) {
    final FTPClient client = new FTPClient();
    // issue#I3O81Y@Gitee
    client.setRemoteVerificationEnabled(false);
    final Charset charset = config.getCharset();
    if (null != charset) {
        client.setControlEncoding(charset.toString());
    }
    client.setConnectTimeout((int) config.getConnectionTimeout());
    final String systemKey = config.getSystemKey();
    if (StrUtil.isNotBlank(systemKey)) {
        final FTPClientConfig conf = new FTPClientConfig(systemKey);
        final String serverLanguageCode = config.getServerLanguageCode();
        if (StrUtil.isNotBlank(serverLanguageCode)) {
            conf.setServerLanguageCode(config.getServerLanguageCode());
        }
        client.configure(conf);
    }
    // connect
    try {
        // 连接ftp服务器
        client.connect(config.getHost(), config.getPort());
        client.setSoTimeout((int) config.getSoTimeout());
        // 登录ftp服务器
        client.login(config.getUser(), config.getPassword());
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    // 是否成功登录服务器
    final int replyCode = client.getReplyCode();
    if (false == FTPReply.isPositiveCompletion(replyCode)) {
        try {
            client.disconnect();
        } catch (IOException e) {
        // ignore
        }
        throw new FtpException("Login failed for user [{}], reply code is: [{}]", config.getUser(), replyCode);
    }
    this.client = client;
    if (mode != null) {
        setMode(mode);
    }
    return this;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) FTPClientConfig(org.apache.commons.net.ftp.FTPClientConfig) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 57 with FTPClient

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

Example 58 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project alfresco-repository by Alfresco.

the class FTPServerTest method testTwoUserUpdate.

// test Rename Case
/**
 * Create a user other than "admin" who has access to a set of files.
 *
 * Create a folder containing test.docx as user one
 * Update that file as user two.
 * Check user one can see user two's changes.
 *
 * @throws Exception
 */
public void testTwoUserUpdate() throws Exception {
    logger.debug("Start testFTPConnect");
    final String TEST_DIR = "/Alfresco/User Homes/" + USER_ONE;
    FTPClient ftpOne = connectClient();
    FTPClient ftpTwo = connectClient();
    try {
        int reply = ftpOne.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            fail("FTP server refused connection.");
        }
        reply = ftpTwo.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            fail("FTP server refused connection.");
        }
        boolean login = ftpOne.login(USER_ONE, PASSWORD_ONE);
        assertTrue("user one login not successful", login);
        login = ftpTwo.login(USER_TWO, PASSWORD_TWO);
        assertTrue("user two login not successful", login);
        boolean success = ftpOne.changeWorkingDirectory("Alfresco");
        assertTrue("user one unable to cd to Alfreco", success);
        success = ftpOne.changeWorkingDirectory("User*Homes");
        assertTrue("user one unable to cd to User*Homes", success);
        success = ftpOne.changeWorkingDirectory(USER_ONE);
        assertTrue("user one unable to cd to " + USER_ONE, success);
        success = ftpTwo.changeWorkingDirectory("Alfresco");
        assertTrue("user two unable to cd to Alfreco", success);
        success = ftpTwo.changeWorkingDirectory("User*Homes");
        assertTrue("user two unable to cd to User*Homes", success);
        success = ftpTwo.changeWorkingDirectory(USER_ONE);
        assertTrue("user two unable to cd " + USER_ONE, success);
        // Create a file as user one
        String FILE1_CONTENT_1 = "test file 1 content";
        String FILE1_NAME = "test.docx";
        success = ftpOne.appendFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_1.getBytes("UTF-8")));
        assertTrue("user one unable to append file", success);
        // Update the file as user two
        String FILE1_CONTENT_2 = "test file content updated";
        success = ftpTwo.storeFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8")));
        assertTrue("user two unable to append file", success);
        // User one should read user2's content
        InputStream is1 = ftpOne.retrieveFileStream(FILE1_NAME);
        assertNotNull("is1 is null", is1);
        String content1 = inputStreamToString(is1);
        assertEquals("Content is not as expected", FILE1_CONTENT_2, content1);
        ftpOne.completePendingCommand();
        // User two should read user2's content
        InputStream is2 = ftpTwo.retrieveFileStream(FILE1_NAME);
        assertNotNull("is2 is null", is2);
        String content2 = inputStreamToString(is2);
        assertEquals("Content is not as expected", FILE1_CONTENT_2, content2);
        ftpTwo.completePendingCommand();
        logger.debug("Test finished");
    } finally {
        ftpOne.dele(TEST_DIR);
        if (ftpOne != null) {
            ftpOne.disconnect();
        }
        if (ftpTwo != null) {
            ftpTwo.disconnect();
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 59 with FTPClient

use of org.apache.commons.net.ftp.FTPClient 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();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 60 with FTPClient

use of org.apache.commons.net.ftp.FTPClient 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();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Aggregations

FTPClient (org.apache.commons.net.ftp.FTPClient)128 IOException (java.io.IOException)78 FTPFile (org.apache.commons.net.ftp.FTPFile)36 Test (org.junit.Test)25 InputStream (java.io.InputStream)20 FrameworkException (org.structr.common.error.FrameworkException)20 Tx (org.structr.core.graph.Tx)20 FtpTest (org.structr.web.files.FtpTest)20 File (java.io.File)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 FileInputStream (java.io.FileInputStream)10 FileOutputStream (java.io.FileOutputStream)8 OutputStream (java.io.OutputStream)5 UnknownHostException (java.net.UnknownHostException)5 ConnectException (java.net.ConnectException)4 PrintCommandListener (org.apache.commons.net.PrintCommandListener)4 FTPSClient (org.apache.commons.net.ftp.FTPSClient)4 FTPUtils (com.cas.sim.tis.util.FTPUtils)3 FileNotFoundException (java.io.FileNotFoundException)3 MalformedURLException (java.net.MalformedURLException)3