1

I have tried to list the files in the local path using the library smbj, But unable to list & copy the local files to the remote file server. I am getting the error. Shared the Error Log for reference.

My requirement is to change the "jcifs library" to "smbj library", the below sample code has been written for that.

Code:

import com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation;
import com.hierynomus.smbj.SMBClient;
import com.hierynomus.smbj.SmbConfig;
import com.hierynomus.smbj.auth.AuthenticationContext;
import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.share.DiskShare;

public class test1 {
    
    public static void main(String[] args) throws Exception {

        try {
            SmbConfig cfg = SmbConfig.builder()
                .withMultiProtocolNegotiate(true)
                .withSigningRequired(false)
                .withDfsEnabled(false)
                .build();
        
            final String Local_SHARE_NAME = "/"; 
            final String LOCAL_PATH = "home//usename//myfolder";
            SMBClient client = new SMBClient(cfg);

            try (Connection connection = client.connect("Remote_Server_IP")) { //x.x.x.x
                AuthenticationContext ac = new AuthenticationContext("user", "pwd".toCharArray(), "Domainname");
                Session session = connection.authenticate(ac);

                try (DiskShare share = (DiskShare) session.connectShare(Local_SHARE_NAME)) {
                    for (FileIdBothDirectoryInformation f : share.list(LOCAL_PATH)) {
                        System.out.println("File : " + f.getFileName());
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                client.close();
            }
        } 
    }
}

Error:

[main] INFO com.hierynomus.smbj.connection.PacketEncryptor - Initialized PacketEncryptor with Cipher
[main] INFO com.hierynomus.smbj.connection.Connection - Successfully connected to: x.x.x.x
[main] INFO com.hierynomus.smbj.connection.SMBSessionBuilder - Successfully authenticated xxxx on x.x.x.x, session is 164423090532534600
com.hierynomus.mssmb2.SMBApiException: STATUS_BAD_NETWORK_NAME (0xc00000cc): Could not connect to \\x.x.x.x\/
    at com.hierynomus.smbj.session.Session.connectTree(Session.java:151)
[main] INFO com.hierynomus.smbj.session.Session - Logging off session 164423090532534600 from host x.x.x.x
[main] INFO com.hierynomus.smbj.connection.Connection - Closed connection to x.x.x.x
[Packet Reader for x.x.x.x] INFO com.hierynomus.smbj.transport.tcp.direct.DirectTcpPacketReader - Thread[Packet Reader for x.x.x.x,5,main] stopped.
[main] INFO com.hierynomus.smbj.SMBClient - Going to close all remaining connections
Hulk
  • 6,399
  • 1
  • 30
  • 52
Maria
  • 297
  • 1
  • 5
  • 18
  • So, the relevant part seems to be that you get an `SMBApiException` with message `STATUS_BAD_NETWORK_NAME `. – Hulk Oct 04 '21 at 14:34
  • I am new to smbj. So I searched file copy format for local to remove server. But I didn't get. – Maria Oct 04 '21 at 14:44
  • how to connect the local system and read a list of files and copy the files into a remote server using smbj. – Maria Oct 04 '21 at 17:40
  • My requirement is to list the files present in the folder in local desktop and then the listed file should be copied to a remote server. Searched for the example code and didn't find any. Is authentication required to connect the local directory? if yes what could be the credentials, Is it user credentials? connection = client.connect("127.0.0.1") session = connection.authenticate(new AuthenticationContext("smbj", "smbj".toCharArray(), null)) – Maria Oct 05 '21 at 17:40
  • You don't need SMBJ to get files from local dir... See this [SO thread](https://stackoverflow.com/questions/1844688/how-to-read-all-files-in-a-folder-from-java) – Jokkeri Dec 10 '21 at 06:44
  • For uploading a file with SMBJ, check eg. this [OS answer](https://stackoverflow.com/a/68723855/2996452), or if you need to APPEND then see this [OS answer](https://stackoverflow.com/a/66817224/2996452) – Jokkeri Dec 10 '21 at 06:49

0 Answers0