From f8491f698350ae582a944f01ff21bf54590dfffa Mon Sep 17 00:00:00 2001 From: MattBrou Date: Thu, 2 May 2019 13:38:15 -0400 Subject: [PATCH] Added an example Took me a while to figure out how to properly read a file with SMBJ library. So I thought I'd share my solution. --- README.adoc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.adoc b/README.adoc index 1b855954..979ea773 100644 --- a/README.adoc +++ b/README.adoc @@ -87,6 +87,34 @@ C - Adjusting Timeout and Socket Timeout } } +``` +D - Reading a file +```java + + SMBClient client = new SMBClient(config); + + 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")) { + + //Opens the file + File smbFileRead = share.openFile("PATH\\FILENAME", EnumSet.of(AccessMask.FILE_READ_DATA), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null); + + //Creates a buffered reader + BufferedReader reader = new BufferedReader(new InputStreamReader(smbFileRead.getInputStream())); + + //Reads the content line by line + String content = reader.lines() + .collect(Collectors.joining(System.lineSeparator())) + + //'content' holds the file's content + return content; + } + } + ``` == Frequently Asked Questions