3

I am trying to use the SMBJ jar (https://github.com/hierynomus/smbj) to fetch a file from shared drive. I am able to connect to the shared disk. After the connect, any operation I perform is throwing exception.

For my shared drive location: //x.x.x.x/containing/folder/filename.txt, I am using the parameters as:

//SERVER_ADDRESS = x.x.x.x
//SHARE_NAME = /containing/folder/
//FILE_NAME = filename.txt
//LOCAL_PATH = "D:\\";

    SMBClient client = new SMBClient();
    try (Connection connection = client.connect(SERVER_ADDRESS)) {
        AuthenticationContext ac = new AuthenticationContext(USERNAME, PASSWORD.toCharArray(), DOMAIN);
        Session session = connection.authenticate(ac);
        // Connect to Share
        try (DiskShare share = (DiskShare) session.connectShare(SHARE_NAME)) {
            Set<SMB2ShareAccess> s = new HashSet<>();
            s.add(SMB2ShareAccess.ALL.iterator().next()); // this is to get READ only
            com.hierynomus.smbj.share.File remoteSmbjFile =  share.openFile(SHARE_NAME+FILE_NAME, EnumSet.of(AccessMask.GENERIC_READ), null, s, null, null);
            java.io.File dest = new java.io.File(LOCAL_PATH + FILE_NAME);
            try (InputStream is = remoteSmbjFile.getInputStream();
                    OutputStream os = new FileOutputStream(dest);) {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        LOGGER.error("", e);
        return "ERROR";
    } catch (Exception e) {
        e.printStackTrace();
        LOGGER.error("", e);
        return "ERROR";
    }

I am getting the exception as below. I think that I am not getting the right combination for the parameters.

com.hierynomus.mssmb2.SMBApiException: STATUS_INVALID_PARAMETER(3221225485/3221225485): Create failed for \containing\folder\
    at com.hierynomus.smbj.share.Share.receive(Share.java:364)
    at com.hierynomus.smbj.share.Share.sendReceive(Share.java:344)
    at com.hierynomus.smbj.share.Share.createFile(Share.java:136)
    at com.hierynomus.smbj.share.DiskShare.open(DiskShare.java:59)
    at com.hierynomus.smbj.share.DiskShare.openDirectory(DiskShare.java:82)
    at com.hierynomus.smbj.share.DiskShare.list(DiskShare.java:169)
    at com.hierynomus.smbj.share.DiskShare.list(DiskShare.java:151)

I am unable to get what is wrong here. Can you please suggest what I am missing here.

alpha
  • 31
  • 1
  • 2
  • 6

3 Answers3

4

The share name should not contain a \. It should be set to containing in your example. The file name to open should be folder\filename.txt. Also remember, SMB paths use \ and not /.

Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
  • What exactly is the "Share Name"? Also, when adding the filepath, do I need "smb:\\" in front? Any help would be greatly appreciated! – Smitty-Werben-Jager-Manjenson Nov 15 '19 at 20:39
  • Share Name is the name of the SMB share. It is not a URL, so no SMB:// – Hiery Nomus Nov 18 '19 at 09:22
  • Thanks for that information. I haven't been able to test it yet because I am getting a `NoClassDefFound` Error on `SMBClient client = new SMBClient()`. If you get time and feel up for it, could you look at the issue I am having trying to add your SMBJ library to my existing project [here?](https://stackoverflow.com/questions/58905579/android-studio-implement-smbj-in-existing-project). I'm not nearly as fluent in Android Studio as I am Visual Studio. – Smitty-Werben-Jager-Manjenson Nov 18 '19 at 14:55
0

if your share folder is \\x.x.x.x\containing\folder\, then shareName is containing

KevinBui
  • 880
  • 15
  • 27
0

Try to use https://github.com/AgNO3/jcifs-ng library, if it isn't necessary to use the one in your code.

A working connection to shared folder with SmbFile class: SmbException failed to connect hostname/IP_address throwing with proper credentials in Java

Soma Básthy
  • 110
  • 1
  • 8