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:Main.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();

    client.connect("ftp.domain.com");
    client.login("admin", "secret");
    String filename = "/testing/data.txt";
    boolean deleted = client.deleteFile(filename);
    if (deleted) {
        System.out.println("File deleted...");
    }/*from   w  w  w . j  a  v a2 s  .  c  om*/

    client.logout();
    client.disconnect();
}

From source file:com.ephesoft.dcma.util.FTPUtil.java

/**
 * API for deleting the data existing in ftp directory on the ftp server.
 * // ww w . j a  v a  2  s.c  o  m
 * @param client {@link FTPClient}
 * @param ftpDirectory {@link String}
 * @param isSourceFolderDeleted
 */
public static void deleteExistingFTPData(final FTPClient client, final String ftpDirectory,
        final boolean isSourceFolderDeleted) {
    if (client != null && ftpDirectory != null) {
        try {
            FTPFile[] ftpFileList = client.listFiles(ftpDirectory);
            for (FTPFile ftpFile : ftpFileList) {
                client.deleteFile(
                        EphesoftStringUtil.concatenate(ftpDirectory, File.separator, ftpFile.getName()));
            }
            if (isSourceFolderDeleted) {
                client.removeDirectory(ftpDirectory);
            }
        } catch (IOException e) {
            LOGGER.error(EphesoftStringUtil.concatenate("Error in deleting existing file on ftp server ",
                    e.getMessage(), e));
        }
    }
}

From source file:eu.ggnet.dwoss.misc.op.listings.FtpTransfer.java

private static void deleteFilesByType(FTPClient ftp, Collection<String> fileTypesToDelete) throws IOException {
    if (fileTypesToDelete == null || fileTypesToDelete.isEmpty())
        return;//from  w w  w.j av a  2s  . c  o  m
    FTPFile[] existingFiles = ftp.listFiles();
    if (existingFiles == null || existingFiles.length == 0)
        return;
    for (String type : fileTypesToDelete) {
        for (FTPFile ftpFile : existingFiles) {
            if (ftpFile == null)
                continue;
            if (ftpFile.getName().toLowerCase().endsWith(type)) {
                if (!ftp.deleteFile(ftpFile.getName())) {
                    throw new IOException("Could not delete " + ftpFile.getName());
                }
            }
        }
    }
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

private static void cleanupWorkerData(FTPClient ftp) throws IOException {
    ftp.deleteFile(ftpRootPath + aspScriptName);
    ftp.deleteFile(ftpRootPath + "jdk.zip");
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static void removeCustomJdkArtifacts(FTPClient ftp, IProgressIndicator pi) throws IOException {
    if (doesRemoteFolderExist(ftp, ftpRootPath, jdkFolderName)) {
        removeFtpDirectory(ftp, ftpJdkPath, pi);
    }/*from w w w  . ja va  2s  .  co m*/
    ftp.deleteFile(ftpRootPath + webConfigFilename);
    ftp.deleteFile(ftpRootPath + reportFilename);
    ftp.deleteFile(ftpRootPath + statusFilename);
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static void deployArtifact(String artifactName, String artifactPath, PublishingProfile pp,
        boolean toRoot, IProgressIndicator indicator) throws IOException {
    FTPClient ftp = null;
    InputStream input = null;//  ww  w  . java  2  s . c  om
    try {
        if (indicator != null)
            indicator.setText("Connecting to FTP server...");

        ftp = getFtpConnection(pp);

        if (indicator != null)
            indicator.setText("Uploading the application...");
        input = new FileInputStream(artifactPath);
        if (toRoot) {
            WebAppUtils.removeFtpDirectory(ftp, ftpWebAppsPath + "ROOT", indicator);
            ftp.deleteFile(ftpWebAppsPath + "ROOT.war");
            ftp.storeFile(ftpWebAppsPath + "ROOT.war", input);
        } else {
            WebAppUtils.removeFtpDirectory(ftp, ftpWebAppsPath + artifactName, indicator);
            ftp.deleteFile(artifactName + ".war");
            boolean success = ftp.storeFile(ftpWebAppsPath + artifactName + ".war", input);
            if (!success) {
                int rc = ftp.getReplyCode();
                throw new IOException("FTP client can't store the artifact, reply code: " + rc);
            }
        }
        if (indicator != null)
            indicator.setText("Logging out of FTP server...");
        ftp.logout();
    } finally {
        if (input != null)
            input.close();
        if (ftp != null && ftp.isConnected()) {
            ftp.disconnect();
        }
    }
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

private static void uploadWebConfigForCustomJdk(FTPClient ftp, WebApp webApp, String jdkFolderName,
        WebContainer webContainer, IProgressIndicator indicator) throws IOException {
    if (jdkFolderName == null || jdkFolderName.isEmpty()) {
        throw new IllegalArgumentException("jdkFolderName is null or empty");
    }/*w ww.  ja  v  a  2s .  co m*/

    if (indicator != null)
        indicator.setText("Stopping the service...");
    webApp.stop();

    if (indicator != null)
        indicator.setText("Deleting " + webConfigFilename + "...");
    ftp.deleteFile(ftpRootPath + webConfigFilename);

    if (indicator != null)
        indicator.setText("Turning the App Service into java based...");
    webApp.update().withJavaVersion(JavaVersion.JAVA_8_NEWEST).withWebContainer(webContainer).apply();

    if (indicator != null)
        indicator.setText("Generating " + webConfigFilename + "...");
    String jdkPath = "%HOME%\\site\\wwwroot\\jdk\\" + jdkFolderName;
    String webContainerPath = generateWebContainerPath(webContainer);
    byte[] webConfigData = generateWebConfigForCustomJDK(jdkPath, webContainerPath);

    if (indicator != null)
        indicator.setText("Uploading " + webConfigFilename + "...");
    ftp.storeFile(ftpRootPath + webConfigFilename, new ByteArrayInputStream(webConfigData));

    if (indicator != null)
        indicator.setText("Starting the service...");
    webApp.start();
}

From source file:com.globalsight.smartbox.util.FtpHelper.java

public boolean ftpDeleteFile(String p_fileName) {
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        try {/* ww  w .j a  va  2  s. co  m*/
            return ftpClient.deleteFile(p_fileName);
        } catch (IOException e) {
            LogUtil.fail("Delete File Error: " + p_fileName, e);
        } finally {
            closeFtpClient(ftpClient);
        }
    }

    return false;
}

From source file:cn.zhuqi.mavenssh.web.util.FTPClientTemplate.java

/**
 * ?//from w w  w .j  av a 2  s .  com
 *
 * @param delFiles
 * @param autoClose ??
 *
 * @return
 * @throws Exception
 */
public boolean delete(String[] delFiles, boolean autoClose) throws Exception {
    try {
        FTPClient ftpClient = getFTPClient();
        for (String s : delFiles) {
            ftpClient.deleteFile(s);
        }
        return true;
    } catch (IOException e) {
        throw new Exception("Couldn't delete file from server.", e);
    } finally {
        if (autoClose) {
            disconnect(); //
        }
    }
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static void removeFtpDirectory(FTPClient ftpClient, String path, IProgressIndicator pi)
        throws IOException {
    String prefix = "Removing from FTP server: ";
    FTPFile[] subFiles = ftpClient.listFiles(path);
    if (subFiles.length > 0) {
        for (FTPFile ftpFile : subFiles) {
            if (pi != null && pi.isCanceled())
                break;
            String currentFileName = ftpFile.getName();
            if (currentFileName.equals(".") || currentFileName.equals("..")) {
                continue; // skip
            }//from www  . j  a v a 2s .c o  m

            String path1 = path + "/" + currentFileName;
            if (ftpFile.isDirectory()) {
                // remove the sub directory
                removeFtpDirectory(ftpClient, path1, pi);
            } else {
                // delete the file
                if (pi != null)
                    pi.setText2(prefix + path1);
                ftpClient.deleteFile(path1);
            }
        }
    }

    if (pi != null)
        pi.setText2(prefix + path);
    ftpClient.removeDirectory(path);
    if (pi != null)
        pi.setText2("");
}