seristee
seristee

Reputation: 1

Write multipart file to remote server with SMBJ

I am new to SMBJ and i would like to know how do i write a file to an smb share using SMBJ found at https://github.com/hierynomus/smbj.

I can create a text file with the following code but i want to write a multipart file from a form to a remote server.

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

Upvotes: 0

Views: 1952

Answers (0)

Related Questions