Example usage for org.springframework.integration.file.remote RemoteFileUtils makeDirectories

List of usage examples for org.springframework.integration.file.remote RemoteFileUtils makeDirectories

Introduction

In this page you can find the example usage for org.springframework.integration.file.remote RemoteFileUtils makeDirectories.

Prototype

public static synchronized <F> void makeDirectories(String path, Session<F> session, String remoteFileSeparator,
        Log logger) throws IOException 

Source Link

Document

Recursively create remote directories.

Usage

From source file:org.springframework.integration.file.remote.RemoteFileTemplate.java

@Override
public void rename(final String fromPath, final String toPath) {
    Assert.hasText(fromPath, "Old filename cannot be null or empty");
    Assert.hasText(toPath, "New filename cannot be null or empty");

    this.execute((SessionCallbackWithoutResult<F>) session -> {
        int lastSeparator = toPath.lastIndexOf(RemoteFileTemplate.this.remoteFileSeparator);
        if (lastSeparator > 0) {
            String remoteFileDirectory = toPath.substring(0, lastSeparator + 1);
            RemoteFileUtils.makeDirectories(remoteFileDirectory, session,
                    RemoteFileTemplate.this.remoteFileSeparator, RemoteFileTemplate.this.logger);
        }/*w  w  w  . j  a  v a 2  s. co  m*/
        session.rename(fromPath, toPath);
    });
}

From source file:org.springframework.integration.file.remote.RemoteFileTemplate.java

private void sendFileToRemoteDirectory(InputStream inputStream, String temporaryRemoteDirectory,
        String remoteDirectory, String fileName, Session<F> session, FileExistsMode mode) throws IOException {

    remoteDirectory = this.normalizeDirectoryPath(remoteDirectory);
    temporaryRemoteDirectory = this.normalizeDirectoryPath(temporaryRemoteDirectory);

    String remoteFilePath = remoteDirectory + fileName;
    String tempRemoteFilePath = temporaryRemoteDirectory + fileName;
    // write remote file first with temporary file extension if enabled

    String tempFilePath = tempRemoteFilePath + (this.useTemporaryFileName ? this.temporaryFileSuffix : "");

    if (this.autoCreateDirectory) {
        try {//from  ww  w .  j a  va  2 s .c o m
            RemoteFileUtils.makeDirectories(remoteDirectory, session, this.remoteFileSeparator, this.logger);
        } catch (IllegalStateException e) {
            // Revert to old FTP behavior if recursive mkdir fails, for backwards compatibility
            session.mkdir(remoteDirectory);
        }
    }

    try {
        boolean rename = this.useTemporaryFileName;
        if (FileExistsMode.REPLACE.equals(mode)) {
            session.write(inputStream, tempFilePath);
        } else if (FileExistsMode.APPEND.equals(mode)) {
            session.append(inputStream, tempFilePath);
        } else {
            if (exists(remoteFilePath)) {
                if (FileExistsMode.FAIL.equals(mode)) {
                    throw new MessagingException(
                            "The destination file already exists at '" + remoteFilePath + "'.");
                } else {
                    if (this.logger.isDebugEnabled()) {
                        this.logger.debug("File not transferred to '" + remoteFilePath + "'; already exists.");
                    }
                }
                rename = false;
            } else {
                session.write(inputStream, tempFilePath);
            }
        }
        // then rename it to its final name if necessary
        if (rename) {
            session.rename(tempFilePath, remoteFilePath);
        }
    } catch (Exception e) {
        throw new MessagingException("Failed to write to '" + tempFilePath + "' while uploading the file", e);
    } finally {
        inputStream.close();
    }
}