sftpvfs2同步目录创建

p5fdfcr1  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(172)

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

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题