Sftp vfs2 同步目录创建

问题描述

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

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 也有同样的效果。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...