Запись составного файла на удаленный сервер с SMBJ - PullRequest
0 голосов
/ 17 мая 2019

Я новичок в SMBJ, и я хотел бы знать, как мне записать файл в общий ресурс smb, используя SMBJ, найденный по адресу https://github.com/hierynomus/smbj.

Я могу создать текстовый файл со следующим кодом, но я хочузаписать составной файл из формы на удаленный сервер.

    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();
    }
...