Example usage for org.apache.commons.net.ftp FTPClient deleteFile

List of usage examples for org.apache.commons.net.ftp FTPClient deleteFile

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClient deleteFile.

Prototype

public boolean deleteFile(String pathname) throws IOException 

Source Link

Document

Deletes a file on the FTP server.

Usage

From source file:org.alinous.ftp.FtpManager.java

public void deleteFile(String sessionId, String pathname) throws IOException {
    FTPClient ftp = this.instances.get(sessionId).getFtp();
    ftp.deleteFile(pathname);
}

From source file:org.apache.activemq.blob.FTPBlobDownloadStrategy.java

public void deleteFile(ActiveMQBlobMessage message) throws IOException, JMSException {
    url = message.getURL();//  w w w. jav a 2s  .c o m
    final FTPClient ftp = createFTP();

    String path = url.getPath();
    try {
        if (!ftp.deleteFile(path)) {
            throw new JMSException("Delete file failed: " + ftp.getReplyString());
        }
    } finally {
        ftp.quit();
        ftp.disconnect();
    }

}

From source file:org.apache.ftpserver.clienttests.SiteTest.java

public void testSiteStat() throws Exception {
    // reboot server to clear stats
    server.stop();// w w  w.  java  2  s  . c  o  m
    initServer();

    // let's generate some stats
    FTPClient client1 = new FTPClient();
    client1.connect("localhost", getListenerPort());

    assertTrue(client1.login(ADMIN_USERNAME, ADMIN_PASSWORD));
    assertTrue(client1.makeDirectory("foo"));
    assertTrue(client1.makeDirectory("foo2"));
    assertTrue(client1.removeDirectory("foo2"));
    assertTrue(client1.storeFile(TEST_FILENAME, new ByteArrayInputStream(TESTDATA)));
    assertTrue(client1.storeFile(TEST_FILENAME, new ByteArrayInputStream(TESTDATA)));
    assertTrue(client1.retrieveFile(TEST_FILENAME, new ByteArrayOutputStream()));
    assertTrue(client1.deleteFile(TEST_FILENAME));

    assertTrue(client1.logout());
    client1.disconnect();

    FTPClient client2 = new FTPClient();
    client2.connect("localhost", getListenerPort());

    assertTrue(client2.login(ANONYMOUS_USERNAME, ANONYMOUS_PASSWORD));
    // done setting up stats

    client.connect("localhost", getListenerPort());
    client.login(ADMIN_USERNAME, ADMIN_PASSWORD);

    client.sendCommand("SITE STAT");
    String[] siteReplies = client.getReplyString().split("\r\n");

    assertEquals("200-", siteReplies[0]);

    String pattern = "Start Time               : " + TIMESTAMP_PATTERN;
    assertTrue(Pattern.matches(pattern, siteReplies[1]));

    assertTrue(Pattern.matches("File Upload Number       : 2", siteReplies[2]));
    assertTrue(Pattern.matches("File Download Number     : 1", siteReplies[3]));
    assertTrue(Pattern.matches("File Delete Number       : 1", siteReplies[4]));
    assertTrue(Pattern.matches("File Upload Bytes        : 16", siteReplies[5]));
    assertTrue(Pattern.matches("File Download Bytes      : 8", siteReplies[6]));
    assertTrue(Pattern.matches("Directory Create Number  : 2", siteReplies[7]));
    assertTrue(Pattern.matches("Directory Remove Number  : 1", siteReplies[8]));
    assertTrue(Pattern.matches("Current Logins           : 2", siteReplies[9]));
    assertTrue(Pattern.matches("Total Logins             : 3", siteReplies[10]));
    assertTrue(Pattern.matches("Current Anonymous Logins : 1", siteReplies[11]));
    assertTrue(Pattern.matches("Total Anonymous Logins   : 1", siteReplies[12]));
    assertTrue(Pattern.matches("Current Connections      : 2", siteReplies[13]));
    assertTrue(Pattern.matches("200 Total Connections        : 3", siteReplies[14]));
}

From source file:org.apache.hadoop.fs.ftp.FTPFileSystem.java

/**
 * 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.
 *///from  w w  w.j a  v  a2s.com
private boolean delete(FTPClient client, Path file, boolean recursive) throws IOException {
    Path workDir = new Path(client.printWorkingDirectory());
    Path absolute = makeAbsolute(workDir, file);
    String pathName = absolute.toUri().getPath();
    FileStatus fileStat = getFileStatus(client, absolute);
    if (!fileStat.isDir()) {
        return client.deleteFile(pathName);
    }
    FileStatus[] dirEntries = listStatus(client, absolute);
    if (dirEntries != null && dirEntries.length > 0 && !(recursive)) {
        throw new IOException("Directory: " + file + " is not empty.");
    }
    if (dirEntries != null) {
        for (int i = 0; i < dirEntries.length; i++) {
            delete(client, new Path(absolute, dirEntries[i].getPath()), recursive);
        }
    }
    return client.removeDirectory(pathName);
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTP.java

/**
 * auto find the time difference between local and remote
 * @param ftp handle to ftp client//from www. ja  va 2s.co m
 * @return number of millis to add to remote time to make it comparable to local time
 * @since ant 1.6
 */
private long getTimeDiff(FTPClient ftp) {
    long returnValue = 0;
    File tempFile = findFileName(ftp);
    try {
        // create a local temporary file
        FILE_UTILS.createNewFile(tempFile);
        long localTimeStamp = tempFile.lastModified();
        BufferedInputStream instream = new BufferedInputStream(new FileInputStream(tempFile));
        ftp.storeFile(tempFile.getName(), instream);
        instream.close();
        boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
        if (success) {
            FTPFile[] ftpFiles = ftp.listFiles(tempFile.getName());
            if (ftpFiles.length == 1) {
                long remoteTimeStamp = ftpFiles[0].getTimestamp().getTime().getTime();
                returnValue = localTimeStamp - remoteTimeStamp;
            }
            ftp.deleteFile(ftpFiles[0].getName());
        }
        // delegate the deletion of the local temp file to the delete task
        // because of race conditions occurring on Windows
        Delete mydelete = new Delete();
        mydelete.bindToOwner(this);
        mydelete.setFile(tempFile.getCanonicalFile());
        mydelete.execute();
    } catch (Exception e) {
        throw new BuildException(e, getLocation());
    }
    return returnValue;
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * auto find the time difference between local and remote
 * @param ftp handle to ftp client/*  ww  w.  j a  va 2s.c  o  m*/
 * @return number of millis to add to remote time to make it comparable to local time
 * @since ant 1.6
 */
private long getTimeDiff(FTPClient ftp) {
    long returnValue = 0;
    File tempFile = findFileName(ftp);
    try {
        // create a local temporary file
        FILE_UTILS.createNewFile(tempFile);
        long localTimeStamp = tempFile.lastModified();
        BufferedInputStream instream = new BufferedInputStream(new FileInputStream(tempFile));
        ftp.storeFile(tempFile.getName(), instream);
        instream.close();
        boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
        if (success) {
            FTPFile[] ftpFiles = ftp.listFiles(tempFile.getName());
            if (ftpFiles.length == 1) {
                long remoteTimeStamp = ftpFiles[0].getTimestamp().getTime().getTime();
                returnValue = localTimeStamp - remoteTimeStamp;
            }
            ftp.deleteFile(ftpFiles[0].getName());
        }
        // delegate the deletion of the local temp file to the delete task
        // because of race conditions occurring on Windows
        Delete mydelete = new Delete();
        mydelete.bindToOwner(task);
        mydelete.setFile(tempFile.getCanonicalFile());
        mydelete.execute();
    } catch (Exception e) {
        throw new BuildException(e, task.getLocation());
    }
    return returnValue;
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * Delete a file from the remote host./*www  . jav a  2 s  .  c  om*/
 * @param ftp ftp client
 * @param filename file to delete
 * @throws IOException  in unknown circumstances
 * @throws BuildException if skipFailedTransfers is set to false
 * and the deletion could not be done
 */
protected void delFile(FTPClient ftp, String filename) throws IOException, BuildException {
    if (task.isVerbose()) {
        task.log("deleting " + filename);
    }

    if (!ftp.deleteFile(resolveFile(filename))) {
        String s = "could not delete file: " + ftp.getReplyString();

        if (task.isSkipFailedTransfers()) {
            task.log(s, Project.MSG_WARN);
            skipped++;
        } else {
            throw new BuildException(s);
        }
    } else {
        task.log("File " + filename + " deleted from " + task.getServer(), Project.MSG_VERBOSE);
        transferred++;
    }
}

From source file:org.covito.kit.file.support.FtpFileServiceImpl.java

/**
 * {@inheritDoc}/*from  www . j a v  a  2  s  . com*/
 * 
 * @author covito
 * @param path
 * @return
 */
@Override
public int deleteFile(String path) {
    init();
    try {
        FTPClient client = getConnect();
        if (!dealDocPath(client, path, false)) {
            return 0;
        }
        boolean sucess = client.deleteFile(getFilePath(path));
        if (!sucess) {
            log.warn(client.getReplyString());
        }
        sucess = client.deleteFile(getMetaPath(path));
        if (!sucess) {
            log.warn(client.getReplyString());
        }
        return 0;
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new FileServiceException(e);
    }
}

From source file:org.jumpmind.metl.core.runtime.resource.FtpDirectory.java

@Override
public boolean delete(String relativePath) {
    FTPClient ftpClient = null;
    try {//from   w w  w  .  ja  v a  2 s . c  o  m
        ftpClient = createClient();
        return ftpClient.deleteFile(relativePath);
    } catch (Exception e) {
        throw new IoException(e);
    } finally {
        FtpDirectory.this.close(ftpClient);

    }
}

From source file:org.limy.common.util.FtpUtils.java

/**
 * ???/*from  ww w.  j  av a 2  s . co  m*/
 * @param client FTP
 * @param relativePath ??
 * @return ????
 * @throws IOException I/O
 */
public static boolean deleteFile(FTPClient client, String relativePath) throws IOException {

    return client.deleteFile(relativePath);
}