Sftp vfs2 同步目录创建

时间:2020-12-29 11:39:49

标签: java ftp sftp vfs

我使用以下方法从远程下载文件。

public String download( String localFilePath, String remoteFilePath) throws WrongConfigurationException{

    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Append _downlaod_from_sftp to the given file name.
        //String downloadFilePath = localFilePath.substring(0, localFilePath.lastIndexOf(".")) + "_downlaod_from_sftp" + localFilePath.substring(localFilePath.lastIndexOf("."), localFilePath.length());

        // Create local file object. Change location if necessary for new downloadFilePath
        FileObject localFile = manager.resolveFile(localFilePath);

        // Create remote file object
        FileObject remoteFile = manager.resolveFile(createConnectionString(remoteFilePath), createDefaultOptions());

        // Copy local file to sftp server
        localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
        
        System.out.println("File download success");
        return FILE_DOWNLOADED;
    } catch (Exception e) {
        throw new WrongConfigurationException(e);
    } finally {
        manager.close();
    }
}

这不是线程安全的,因为当我从不同的线程同时启动两个下载操作时,其中一个失败,因为它们尝试同时创建相同的目录。我怎样才能使它线程安全,以便它可以提供线程安全?

我只是不想将方法标记为同步。 SynchronizedFileObject 也有同样的效果。

0 个答案:

没有答案
相关问题