hierynomus smbj write - share file access denied issue

1.5k Views Asked by At

Using smbj to rite file to shared folder and the below code is giving me access denied issue. But i have mounted the same shared folder under "Network Locations" and i can write file to the same folder by manually creating. I am guessing the options i use in to write the file is not correct. Code:

public static void main(String args[]) {
        SMBClient client = new SMBClient();

        try (Connection connection = client.connect("10.48.36.248")) {
            AuthenticationContext ac = new AuthenticationContext("test", "1234".toCharArray(), "");
            Session session = connection.authenticate(ac);

            // Connect to ShareCards
            try (DiskShare share = (DiskShare) session.connectShare("CCS")) {
                Set<FileAttributes> fileAttributes = new HashSet<>();
                fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);
                Set<SMB2CreateOptions> createOptions = new HashSet<>();
                createOptions.add(SMB2CreateOptions.FILE_NON_DIRECTORY_FILE);
                createOptions.add(SMB2CreateOptions.FILE_WRITE_THROUGH);
                File f = share.openFile(                
                        "\\SWD\\Groups\\New Folder\\"
                                + "Test.txt",
                        new HashSet(Arrays.asList(new AccessMask[] { AccessMask.FILE_ADD_FILE })), fileAttributes,
                        SMB2ShareAccess.ALL,
                        SMB2CreateDisposition.FILE_OVERWRITE_IF, createOptions);

                OutputStream oStream = f.getOutputStream();
                oStream.write("I am testing".getBytes());
                oStream.flush();
                oStream.close();
            
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            client.close();
        }

The exception i am getting is

Exception in thread "main" com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED (0xc0000022): Create failed for \\10.48.36.248\CCS\SWD\Groups\New Folder\Test.txt
    

UPDATE: \\10.48.36.248\CCS\SWD after this the "Groups" folder looks like a shortcut to another location, maybe that is the reason I am getting access denied error? How do i write to the shortcut folder?

2

There are 2 best solutions below

0
Pat On BEST ANSWER

Implemented using jcifs-ng and it works fine

Pom

<dependency>
<groupId>eu.agno3.jcifs</groupId>
<artifactId>jcifs-ng</artifactId>
<version>2.1.7</version>
</dependency>

Code:

import jcifs.CIFSContext;
import jcifs.CIFSException;
import jcifs.context.SingletonContext;
import jcifs.smb.NtlmPasswordAuthenticator;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

public class JCIFSTest {
    public static void main(String[] args) {
        try {
            
            CIFSContext baseCxt = SingletonContext.getInstance();
            CIFSContext auth = baseCxt.withCredentials(new NtlmPasswordAuthenticator("domain", "username", "password"));

            SmbFile f = new SmbFile("smb://10.48.36.248/CCS/SWD/GROUPS/New Folder/test.txt", auth);
            SmbFileOutputStream out = new SmbFileOutputStream(f,true);
            out.write("Just test".getBytes());
            f.close();
            out.close();
        }  catch (CIFSException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
0
Guna On
try (Connection connection = client.connect(address, port)) {
            AuthenticationContext ac = getAuthenticationContext();
            log.info("Authenticating");
String fileContent = "New Test added";
            Session session = connection.authenticate(ac);

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

            }

pom.xml

<dependency>
        <groupId>com.hierynomus</groupId>
        <artifactId>smbj</artifactId>
        <version>0.11.5</version>
    </dependency>

Above code with smbj works to me