首页 > 解决方案 > 使用 SMBJ 将多部分文件写入远程服务器

问题描述

我是 SMBJ 的新手,我想知道如何使用位于https://github.com/hierynomus/smbj的 SMBJ 将文件写入 smb 共享。

我可以使用以下代码创建一个文本文件,但我想将一个多部分文件从表单写入远程服务器。

    String fileName ="Labour Acting Letter.txt";
    String fileContents = "Mary had a little lamb.";
    SMBClient client = new SMBClient();
    try (Connection connection = client.connect("server")) {
        AuthenticationContext ac = new AuthenticationContext("username", "password".toCharArray(), "");
        Session session = connection.authenticate(ac);

        // Connect to Share
        try (DiskShare share = (DiskShare) session.connectShare("fsmis_files")) {
            for (FileIdBothDirectoryInformation f : share.list("FOLDER", "*.*")) {
                System.out.println("File : " + f.getFileName());
            }

            //share.openFile(path, accessMask, attributes, shareAccesses, createDisposition, createOptions)
            Set<FileAttributes> fileAttributes = new HashSet<>();
            fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);
            Set<SMB2CreateOptions> createOptions = new HashSet<>();
            createOptions.add(SMB2CreateOptions.FILE_RANDOM_ACCESS);
            File f = share.openFile("FOLDER"+"\\"+fileName, new HashSet(Arrays.asList(new AccessMask[]{AccessMask.GENERIC_ALL})), fileAttributes, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OVERWRITE_IF, createOptions);
            OutputStream oStream = f.getOutputStream();
            oStream.write(fileContents.getBytes());
            oStream.flush();
            oStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

标签: javaspringhibernatesmbsmbj

解决方案


推荐阅读