1

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 Answers1

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

            }
Guna
  • 11
  • 3
  • Code-only answers tend to be discarded. The difference between answering a question and solving a problem is that the former consists in clarifying through explanation what the answer to a question is, whereas the latter does not necessarily require explanation. Here we are looking for answers rather than solutions. – Lajos Arpad Aug 19 '23 at 17:38
  • @LajosArpad Have answer int his, its working code and pasted above, Issue might be EnumSet.of(SMB2CreateOptions.FILE_RANDOM_ACCESS)), EnumSet.of(AccessMask.GENERIC_WRITE),, replace with above and do let me know – Guna Aug 21 '23 at 19:42
  • You need to explain your answer inside the answer. SO tends to receive better answers that are explained rather than code-only answers. The reason for my original comment was to let you know that your answer would be largely improved if the thought process behind it is explained. If you ask a question, you will likely want to know why a code is claimed to be the solution to it rather than just getting a code. – Lajos Arpad Aug 22 '23 at 10:09