Open mariaprabudass opened 2 years ago
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 /
.
Share examples of how to connect the local system and read a list of files and copy the files into a remote server.
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)
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))
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.
Did this answer your question? If so please close the ticket.
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 ?
Please raise ticket only if there is an issue, if you want to learn already enough examples provided and lots of solved tickets which contains examples you can use. Thanks R Ramarajan
On Sun, 4 Sep, 2022, 10:17 pm Kaushik ramabhotla, @.***> wrote:
Hello @hierynomus https://github.com/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 ?
— Reply to this email directly, view it on GitHub https://github.com/hierynomus/smbj/issues/675#issuecomment-1236377666, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIJCEGSWYGGN5GM6J46MQALV4THDBANCNFSM5FJS4VCQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>
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();
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