首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SMBJ客户端将数据追加到现有文件

使用SMBJ客户端将数据追加到现有文件
EN

Stack Overflow用户
提问于 2021-03-17 00:26:41
回答 2查看 179关注 0票数 0

我需要使用SMBJ客户端将数据追加到现有文件中。下面的代码附加了数据,但最终文件已损坏,无法读取/打开该文件。

代码语言:javascript
复制
 Set<AccessMask> accessMask = new HashSet<>();
 accessMask.add(AccessMask.GENERIC_READ);
 accessMask.add(AccessMask.GENERIC_WRITE);

 Set<FileAttributes> fileAttributes = new HashSet<>();
 fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);

 Set<SMB2CreateOptions> createOptions = new HashSet<>();
 createOptions.add(SMB2CreateOptions.FILE_RANDOM_ACCESS);

 File file = share.openFile("PATH", accessMask, fileAttributes, SMB2ShareAccess.ALL, 
                         SMB2CreateDisposition.FILE_OPEN_IF, createOptions);

 // Approach - 1
 long fileOffset = 0;
 byte[] buffer = new byte[1024*4];
 int length = inputStream.read(buffer);
 while(length != -1){
   fileOffset = share.getFileInformation("PATH").getStandardInformation().getEndOfFile();
   file.write(buffer, fileOffset, 0, lenght);
 }

//Approach - 2
 OutputStream oStream = f.getOutputStream();
     oStream.write(fileContents.getBytes());
     oStream.flush();
     oStream.close();

但从这两种方法来看,都不能正确地附加数据。

EN

回答 2

Stack Overflow用户

发布于 2021-03-26 20:46:01

我是这样做的:

代码语言:javascript
复制
private static void uploadAppend(File source, DiskShare diskShare,
        String destPath) throws IOException {
    if (source != null && source.exists() && source.canRead()
            && source.isFile()) {

        try (InputStream is = new java.io.FileInputStream(source)) {
            if (destPath != null && is != null) {
                Set<AccessMask> accessMask = new HashSet<>(EnumSet.of(
                        AccessMask.FILE_READ_DATA,
                        AccessMask.FILE_WRITE_DATA, AccessMask.DELETE));
                Set<SMB2ShareAccess> shareAccesses = new HashSet<>(
                        EnumSet.of(SMB2ShareAccess.FILE_SHARE_READ,
                                SMB2ShareAccess.FILE_SHARE_WRITE,
                                SMB2ShareAccess.FILE_SHARE_DELETE));
                Set<FileAttributes> createOptions = new HashSet<>(
                        EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL));

                try (com.hierynomus.smbj.share.File file = diskShare
                        .openFile(destPath, accessMask, createOptions,
                                shareAccesses,
                                SMB2CreateDisposition.FILE_OPEN_IF,
                                EnumSet.noneOf(SMB2CreateOptions.class))) {

                    int bufferSize = 2048;
                    if (source.length() > 20971520l) {
                        bufferSize = 131072;
                    }
                    byte[] buffer = new byte[bufferSize];
                    long fileOffset = 0;
                    int length = 0;
                    while ((length = is.read(buffer)) > 0) {
                        fileOffset = diskShare.getFileInformation(destPath)
                                .getStandardInformation().getEndOfFile();
                        file.write(buffer, fileOffset, 0, length);
                    }
                    file.flush();
                    file.close();
                } finally {
                    is.close();
                }
            }
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2021-07-27 21:43:54

下面是一个使用jcifs.smb.SmbFileOutputStreamjcifs.smb.SmbFileInputStream类的解决方案:

代码语言:javascript
复制
private void AppendToFile() throws Exception {
        CIFSContext base = SingletonContext.getInstance();
        CIFSContext context = base.withCredentials(new NtlmPasswordAuthentication(base, "domainName",
                "userName", "password"));
        try (SmbFile f = new SmbFile("smb://serverName/folder/test.txt", context)) {
            SmbFileInputStream in = new SmbFileInputStream(f);
            byte[] buffer = new byte[1024]; //your file size
            int bytesRead = in.read(buffer);
            while (bytesRead > 0) {
                bytesRead = in.read(buffer);
            }
            in.close();

//...
//do your operations with the data in buffer
//...


            SmbFileOutputStream out = f.openOutputStream(true); //true is for append mode
            out.write(buffer);
            out.close();
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66659340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档