Example usage for org.apache.commons.vfs.impl StandardFileSystemManager close

List of usage examples for org.apache.commons.vfs.impl StandardFileSystemManager close

Introduction

In this page you can find the example usage for org.apache.commons.vfs.impl StandardFileSystemManager close.

Prototype

public void close() 

Source Link

Document

Closes all files created by this manager, and cleans up any temporary files.

Usage

From source file:gov.nih.nci.cacis.ip.mirthconnect.ftp.SFTPSender.java

public void sendDocument(InputStream file, String ftpAddress, String extension) {

    StandardFileSystemManager standardFileSystemManager = new StandardFileSystemManager();
    try {/*ww w  .j a  v a  2 s  . com*/
        final FTPInfo ftpInfo = ftpMapping.getFTPInfo(ftpAddress);
        if (ftpInfo == null) {
            throw new ApplicationRuntimeException("No server config exists for address[ " + ftpAddress + " ]");
        }

        String sftpURI = "sftp://" + ftpInfo.getUserName() + ":" + ftpInfo.getPassword() + "@"
                + ftpInfo.getSite() + "/" + ftpInfo.getRootDirectory();
        String fileName = "IHEXIPFTP-" + UUID.randomUUID() + extension;

        FileSystemOptions fileSystemOptions = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fileSystemOptions, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fileSystemOptions, true);

        standardFileSystemManager.init();

        FileObject fileObject = standardFileSystemManager.resolveFile(sftpURI + "/" + fileName,
                fileSystemOptions);

        long timestamp = new Date().getTime();
        String tempSftpFile = sftpFileDirectory + "/" + timestamp + ".xml";
        OutputStream out = new FileOutputStream(new File(tempSftpFile));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = file.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        file.close();
        out.flush();
        out.close();

        FileObject localFileObject = standardFileSystemManager.resolveFile(tempSftpFile);

        fileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);

        FileUtils.forceDelete(new File(tempSftpFile));

    } catch (Exception e) {
        throw new ApplicationRuntimeException("Error sending SFTP. " + e.getMessage());
    } finally {
        standardFileSystemManager.close();
    }
}