Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to list the local files and copy the local files into remote file server #675

Open
mariaprabudass opened this issue Oct 4, 2021 · 9 comments

Comments

@mariaprabudass
Copy link

mariaprabudass commented Oct 4, 2021

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 code & 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:
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());
                }
            }

        }

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

@hierynomus
Copy link
Owner

If you take a look at the examples, you will see that share and path names in the SMB world are always separated with \ and never with /.

@mariaprabudass
Copy link
Author

Share examples of how to connect the local system and read a list of files and copy the files into a remote server.

@hierynomus
Copy link
Owner

def "should read file contents of file in directory"() {

@mariaprabudass
Copy link
Author

mariaprabudass commented Oct 5, 2021

I have tried using Java. I got a "Connection refused" error to connect the local(127.0.0.1) system. What is wrong with the below code. I don't know why authenticate is required to connect local desktop. My requirement is to connect the local desktop and list the files in particular folder using smbj.

Code:

 SmbConfig smbConfig = SmbConfig
      .builder()
      .withMultiProtocolNegotiate(true)
      .withDialects(SMB2Dialect.SMB_3_0).withEncryptData(true)
      .withTransportLayerFactory(new DirectTcpTransportFactory<>())
      .withSecurityProvider(new BCSecurityProvider())
      .withSigningRequired(true)
      /*.withEncryptData(true)*/.build();

    SMBClient client = new SMBClient(smbConfig);
    Connection connection = client.connect("127.0.0.1");
    Session session = connection.authenticate(new AuthenticationContext("smbj", "smbj".toCharArray(), null));
            
            try (DiskShare share = (DiskShare) session.connectShare("/")) {
                for (FileIdBothDirectoryInformation f : share.list("home//username//foldername//")) {
                    System.out.println("File : " + f.getFileName());
                }
            }

Error:
Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.Socket.connect(Socket.java:607)
at com.hierynomus.protocol.commons.socket.ProxySocketFactory.createSocket(ProxySocketFactory.java:87)
at com.hierynomus.protocol.commons.socket.ProxySocketFactory.createSocket(ProxySocketFactory.java:63)
at com.hierynomus.smbj.transport.tcp.direct.DirectTcpTransport.connect(DirectTcpTransport.java:88)
at com.hierynomus.smbj.connection.Connection.connect(Connection.java:139)
at com.hierynomus.smbj.SMBClient.getEstablishedOrConnect(SMBClient.java:96)
at com.hierynomus.smbj.SMBClient.connect(SMBClient.java:71)

@mariaprabudass
Copy link
Author

def "should read file contents of file in directory"() {

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.
The code has been written to list the file in the local desktop. What could be the issue in the above code?
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))

@hierynomus
Copy link
Owner

The exception that you're getting indicates that locally you do not have an SMB server running. I think you should use the regular java.io.File to obtain the file from the local filesystem and then use a java.io.FileInputStream to copy the data to a remote SMB File.

@hierynomus
Copy link
Owner

Did this answer your question? If so please close the ticket.

@kaushikramabhotla
Copy link

Hello @hierynomus ,

I am a beginner, trying to copy some files from a local system to a remote server. I tried sftp, but that wont work in my case. Can you pin point me any code snippet that does this task using smb ?

If this is what you were referring to, I have a few doubts in this snip :

  SMBClient client = new SMBClient();

   try (Connection connection = client.connect("SERVERNAME")) {
       AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");
       Session session = connection.authenticate(ac);

       // Connect to Share
       try (DiskShare share = (DiskShare) session.connectShare("SHARENAME")) {
           for (FileIdBothDirectoryInformation f : share.list("FOLDER", "*.TXT")) {
               System.out.println("File : " + f.getFileName());
           }
       }
   }

what is a SHARENAME and FOLDER in this case ?

@rokkakasu
Copy link

rokkakasu commented Sep 5, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants