0

我正在努力将包含内容的文件重命名为 SMB,使用com.hierynomus.smbj. 文件是随内容生成的,但重命名后内容就消失了。

以下实现重命名文件:

    public void rename(String pathFrom, String pathTo) {
        pathFrom = formatPath(pathFrom);
        pathTo = formatPath(pathTo);
        Set<SMB2ShareAccess> shareAccessSet = new HashSet<>();
        shareAccessSet.add(SMB2ShareAccess.FILE_SHARE_READ);
        shareAccessSet.add(SMB2ShareAccess.FILE_SHARE_WRITE);
        shareAccessSet.add(SMB2ShareAccess.FILE_SHARE_DELETE);

        Set<FileAttributes> fileAttributes = new HashSet<>();
        fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);
        Set<SMB2CreateOptions> createOptions = new HashSet<>();
        SMB2CreateDisposition smb2CreateDisposition = SMB2CreateDisposition.FILE_OVERWRITE_IF;

        if (isFolder(pathFrom)) {
            createOptions.add(SMB2CreateOptions.FILE_DIRECTORY_FILE);
            smb2CreateDisposition = SMB2CreateDisposition.FILE_OPEN_IF;
        }
        else if (isFile(pathFrom)) {
            createOptions.add(SMB2CreateOptions.FILE_NON_DIRECTORY_FILE);
        }
        else {
            throw new IllegalArgumentException("Path '" + pathFrom + "' can't be resolved to file nor directory");
        }
        try (DiskEntry file = this.smbShare.open(pathFrom, of(AccessMask.MAXIMUM_ALLOWED), fileAttributes, shareAccessSet,
                smb2CreateDisposition, createOptions)) {
            file.rename(pathTo, true);
        }
    }

也许我弄乱了smbShare.open-function中的属性和选项?

4

1 回答 1

1

嗯..我搞砸了SMB2CreateDisposition.

解决方案:smb2CreateDisposition = SMB2CreateDisposition.FILE_OPEN_IF; 代替smb2CreateDisposition = SMB2CreateDisposition.FILE_OVERWRITE_IF;

前者打开 (1.) 或创建 (2.) 有问题的文件。后者覆盖现有文件。

于 2021-09-27T13:35:30.590 回答