Example usage for org.apache.commons.io FileUtils moveFile

List of usage examples for org.apache.commons.io FileUtils moveFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils moveFile.

Prototype

public static void moveFile(File srcFile, File destFile) throws IOException 

Source Link

Document

Moves a file.

Usage

From source file:org.syncany.plugins.dropbox.DropboxTransferManager.java

@Override
public void download(RemoteFile remoteFile, File localFile) throws StorageException {
    String remotePath = getRemoteFile(remoteFile);

    if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals("..")) {
        try {/*from   ww  w. jav a 2s.  co  m*/
            // Download file
            File tempFile = createTempFile(localFile.getName());
            OutputStream tempFOS = new FileOutputStream(tempFile);

            if (logger.isLoggable(Level.INFO)) {
                logger.log(Level.INFO, "Dropbox: Downloading {0} to temp file {1}",
                        new Object[] { remotePath, tempFile });
            }

            client.getFile(remotePath, null, tempFOS);

            tempFOS.close();

            // Move file
            if (logger.isLoggable(Level.INFO)) {
                logger.log(Level.INFO, "Dropbox: Renaming temp file {0} to file {1}",
                        new Object[] { tempFile, localFile });
            }

            localFile.delete();
            FileUtils.moveFile(tempFile, localFile);
            tempFile.delete();
        } catch (DbxException | IOException ex) {
            logger.log(Level.SEVERE, "Error while downloading file " + remoteFile.getName(), ex);
            throw new StorageException(ex);
        }
    }
}

From source file:org.syncany.plugins.ftp.FtpTransferManager.java

@Override
public void download(RemoteFile remoteFile, File localFile) throws StorageException {
    connect();/*from   w  ww .j a  v a 2 s  .c om*/

    String remotePath = getRemoteFile(remoteFile);

    try {
        // Download file
        File tempFile = createTempFile(localFile.getName());
        OutputStream tempFOS = new FileOutputStream(tempFile);

        if (logger.isLoggable(Level.INFO)) {
            logger.log(Level.INFO, "FTP: Downloading {0} to temp file {1}",
                    new Object[] { remotePath, tempFile });
        }

        boolean success = ftp.retrieveFile(remotePath, tempFOS);

        if (!success) {
            throw new StorageFileNotFoundException(
                    "Could not find remoteFile to download " + remoteFile.getName());
        }

        tempFOS.close();

        // Move file
        if (logger.isLoggable(Level.INFO)) {
            logger.log(Level.INFO, "FTP: Renaming temp file {0} to file {1}",
                    new Object[] { tempFile, localFile });
        }

        localFile.delete();
        FileUtils.moveFile(tempFile, localFile);
        tempFile.delete();
    } catch (IOException ex) {
        forceFtpDisconnect();

        logger.log(Level.SEVERE, "Error while downloading file " + remoteFile.getName(), ex);
        throw new StorageException(ex);
    }
}

From source file:org.syncany.plugins.googledrive.GoogledriveTransferManager.java

@Override
public void download(RemoteFile remoteFile, File localFile) throws StorageException {
    if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals("..")) {
        try {/*  www  .  j  a  v a 2s. c o  m*/
            // Download file
            File tempFile = createTempFile(localFile.getName());

            if (logger.isLoggable(Level.INFO)) {
                logger.log(Level.INFO, "Google Drive: Downloading {0} to temp file {1}",
                        new Object[] { remoteFile.getName(), tempFile });
            }

            com.google.api.services.drive.model.File curFile = getRemoteFile(remoteFile);

            if (curFile.getDownloadUrl() == null || curFile.getDownloadUrl().length() == 0) {
                throw new StorageException("Cannot get download URL for file");
            }

            HttpResponse resp = drive.getRequestFactory()
                    .buildGetRequest(new GenericUrl(curFile.getDownloadUrl())).execute();

            OutputStream tempFOS = new FileOutputStream(tempFile);

            InputStream tempFIS = resp.getContent();
            IOUtils.copy(tempFIS, tempFOS);

            tempFIS.close();
            tempFOS.close();

            // Move file
            if (logger.isLoggable(Level.INFO)) {
                logger.log(Level.INFO, "Google Drive: Renaming temp file {0} to file {1}",
                        new Object[] { tempFile, localFile });
            }

            localFile.delete();
            FileUtils.moveFile(tempFile, localFile);
            tempFile.delete();
        } catch (IOException | StorageException ex) {
            logger.log(Level.SEVERE, "Error while downloading file " + remoteFile.getName(), ex);
            throw new StorageException(ex);
        }
    }
}

From source file:org.syncany.plugins.local.LocalTransferManager.java

@Override
public void download(RemoteFile remoteFile, File localFile) throws StorageException {
    connect();//from  w  ww .ja v  a  2 s  .c o m

    File repoFile = getRemoteFile(remoteFile);

    if (!repoFile.exists()) {
        throw new StorageFileNotFoundException("No such file in local repository: " + repoFile);
    }

    try {
        File tempLocalFile = createTempFile("local-tm-download");
        tempLocalFile.deleteOnExit();

        FileUtils.copyFile(repoFile, tempLocalFile);

        localFile.delete();
        FileUtils.moveFile(tempLocalFile, localFile);
        tempLocalFile.delete();
    } catch (IOException ex) {
        throw new StorageException("Unable to copy file " + repoFile + " from local repository to " + localFile,
                ex);
    }
}

From source file:org.syncany.plugins.local.LocalTransferManager.java

@Override
public void move(RemoteFile sourceFile, RemoteFile targetFile) throws StorageException {
    connect();/*from  w  w w.j  a  va  2 s . co  m*/

    File sourceRemoteFile = getRemoteFile(sourceFile);
    File targetRemoteFile = getRemoteFile(targetFile);

    if (!sourceRemoteFile.exists()) {
        throw new StorageMoveException("Unable to move file " + sourceFile + " because it does not exist.");
    }

    try {
        FileUtils.moveFile(sourceRemoteFile, targetRemoteFile);
    } catch (IOException ex) {
        throw new StorageException(
                "Unable to move file " + sourceRemoteFile + " to destination " + targetRemoteFile, ex);
    }
}

From source file:org.syncany.plugins.local.LocalTransferManager.java

@Override
public void upload(File localFile, RemoteFile remoteFile) throws StorageException {
    connect();//from   w w  w.j  av  a2 s. co  m

    File repoFile = getRemoteFile(remoteFile);
    File tempRepoFile = new File(
            getAbsoluteParentDirectory(repoFile) + File.separator + ".temp-" + repoFile.getName());

    // Do not overwrite files with same size!
    if (repoFile.exists() && repoFile.length() == localFile.length()) {
        return;
    }

    // No such local file
    if (!localFile.exists()) {
        throw new StorageException("No such file on local disk: " + localFile);
    }

    try {
        FileUtils.copyFile(localFile, tempRepoFile);
        FileUtils.moveFile(tempRepoFile, repoFile);
    } catch (IOException ex) {
        throw new StorageException("Unable to copy file " + localFile + " to local repository " + repoFile, ex);
    }
}

From source file:org.syncany.plugins.s3.S3TransferManager.java

@Override
public void download(RemoteFile remoteFile, File localFile) throws StorageException {
    connect();//from ww w . j a  v a2  s  .c  om

    File tempFile = null;
    String remotePath = getRemoteFile(remoteFile);

    try {
        // Download
        StorageObject fileObj = service.getObject(bucket.getName(), remotePath);
        InputStream fileObjInputStream = fileObj.getDataInputStream();

        logger.log(Level.FINE, "- Downloading from bucket " + bucket.getName() + ": " + fileObj + " ...");
        tempFile = createTempFile(remoteFile.getName());
        FileUtils.copyInputStreamToFile(fileObjInputStream, tempFile);

        fileObjInputStream.close();

        // Move to final location
        if (localFile.exists()) {
            localFile.delete();
        }

        FileUtils.moveFile(tempFile, localFile);
    } catch (Exception ex) {
        if (tempFile != null) {
            tempFile.delete();
        }

        throw new StorageException("Unable to download file '" + remoteFile.getName(), ex);
    }
}

From source file:org.syncany.plugins.samba.SambaTransferManager.java

@Override
public void download(RemoteFile remoteFile, File localFile) throws StorageException {
    if (remoteFile.getName().equals(".") && !remoteFile.getName().equals("..")) {
        return;//from   w w  w. j av  a2  s  .c  o m
    }

    try {
        // Download file
        File tempFile = createTempFile(localFile.getName());
        OutputStream tempFOS = new FileOutputStream(tempFile);
        SmbFile requestedSmbFile = createSmbFile(remoteFile);

        if (logger.isLoggable(Level.INFO)) {
            logger.log(Level.INFO, "Samba: Downloading {0} to temp file {1}",
                    new Object[] { requestedSmbFile.getPath(), tempFile });
        }

        try {
            SmbFileInputStream smbfis = new SmbFileInputStream(requestedSmbFile);
            IOUtils.copy(smbfis, tempFOS);

            tempFOS.close();
            smbfis.close();
        } catch (IOException e) {
            logger.log(Level.WARNING, "Samba: Downloading FAILED. {0} to temp file {1}",
                    new Object[] { requestedSmbFile.getPath(), tempFile });
            throw new StorageFileNotFoundException("Samba: Downloading FAILED: " + requestedSmbFile.getPath(),
                    e);
        }

        // Move file
        if (logger.isLoggable(Level.INFO)) {
            logger.log(Level.INFO, "Samba: Renaming temp file {0} to file {1}",
                    new Object[] { tempFile, localFile });
        }

        localFile.delete();
        FileUtils.moveFile(tempFile, localFile);
        tempFile.delete();
    } catch (IOException ex) {
        logger.log(Level.SEVERE, "Error while downloading file " + remoteFile.getName(), ex);
        throw new StorageException(ex);
    }
}

From source file:org.syncany.plugins.sftp.SftpTransferManager.java

@Override
public void download(RemoteFile remoteFile, File localFile) throws StorageException {
    connect();//from   w w w .  j a  v a 2s. c om

    String remotePath = getRemoteFile(remoteFile);

    if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals("..")) {
        try {
            // Download file
            File tempFile = createTempFile(localFile.getName());
            OutputStream tempFOS = new FileOutputStream(tempFile);

            if (logger.isLoggable(Level.INFO)) {
                logger.log(Level.INFO, "SFTP: Downloading {0} to temp file {1}",
                        new Object[] { remotePath, tempFile });
            }

            sftpChannel.get(remotePath, tempFOS);

            tempFOS.close();

            // Move file
            if (logger.isLoggable(Level.INFO)) {
                logger.log(Level.INFO, "SFTP: Renaming temp file {0} to file {1}",
                        new Object[] { tempFile, localFile });
            }

            localFile.delete();
            FileUtils.moveFile(tempFile, localFile);
            tempFile.delete();
        } catch (SftpException | IOException ex) {
            disconnect();
            logger.log(Level.SEVERE, "Error while downloading file " + remoteFile.getName(), ex);
            throw new StorageException(ex);
        }
    }
}

From source file:org.syncany.plugins.swift.SwiftTransferManager.java

@Override
public void download(RemoteFile remoteFile, File localFile) throws StorageException {
    String remotePath = getRemoteFile(remoteFile);

    if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals("..")) {
        try {/*from   w  ww. j  a  v a  2s  . c o m*/
            // Download file
            File tempFile = createTempFile(localFile.getName());

            if (logger.isLoggable(Level.INFO)) {
                logger.log(Level.INFO, "Swift: Downloading {0} to temp file {1}",
                        new Object[] { remotePath, tempFile });
            }

            this.container.getObject(remotePath).downloadObject(tempFile);

            // Move file
            if (logger.isLoggable(Level.INFO)) {
                logger.log(Level.INFO, "Swift: Renaming temp file {0} to file {1}",
                        new Object[] { tempFile, localFile });
            }

            localFile.delete();
            FileUtils.moveFile(tempFile, localFile);
            tempFile.delete();
        } catch (IOException ex) {
            logger.log(Level.SEVERE, "Error while downloading file " + remoteFile.getName(), ex);
            throw new StorageException(ex);
        }
    }
}