SMBJ unable to write file

1.1k Views Asked by At

Unable to write file to the sahred folder, i get the exception.

com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED (0xc0000022): Create failed for \10.x.x.x\XYZZZZZZ\xyz.txt

I have verified that this account has complete access to that shared folder

// connection params
static String sambaDomain = null;
static String sambaUsername = "user123";
static String sambaPass = "pwd";
static String sambaIP = "10.x.x.x";
static String sambaSharedPath = "XYZZZZZZ";

public static void main(String[] args) {

    SMBClient client = new SMBClient();
    Connection connection = null;
    try {
        byte[] bytes = Files.readAllBytes(Paths.get("C:\\Users\\xxxx\\testdoc2.pdf"));
        connection = client.connect(sambaIP);

        Session session = connection.authenticate(new AuthenticationContext(sambaUsername, sambaPass.toCharArray(), sambaDomain));
        DiskShare share = (DiskShare) session.connectShare(sambaSharedPath);


        File f = share.openFile("xyz.txt",
                EnumSet.of(AccessMask.FILE_WRITE_DATA),
                EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL),
                SMB2ShareAccess.ALL,
                SMB2CreateDisposition.FILE_CREATE,
                EnumSet.of(SMB2CreateOptions.FILE_DIRECTORY_FILE));

        OutputStream os = f.getOutputStream();

        os.write(bytes);
        os.flush();
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
1

There are 1 best solutions below

4
Guna On
try (Connection connection = client.connect(address, port)) {
            AuthenticationContext ac = getAuthenticationContext();
            log.info("Authenticating");
            Session session = connection.authenticate(ac);
            String fileContent = "Content to write";
            log.info("Connecting to share " + shareName);

            try (DiskShare share = (DiskShare) session.connectShare(shareName)) {
                log.info("Move File");

                File file = share.openFile(filePath, EnumSet.of(AccessMask.GENERIC_WRITE), null,
                        SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_CREATE,
                        EnumSet.of(SMB2CreateOptions.FILE_RANDOM_ACCESS));
                {

                    byte[] content = fileContent.getBytes();

                    file.write(content, 0);

                }

                apiMessageList.setStatus(true);

                filesList.add(filePath);
                apiMessageList.setFilesList(filesList);

                share.close();

            } catch (Exception e) {
                e.printStackTrace();

            }