pmiseiko-r7 / smbj

Server Message Block (SMB2, SMB3) implementation in Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

smbj - SMB2/SMB3 client library for Java

To get started, have a look at one of the examples. Hopefully you will find the API pleasant to work with :)

download Build Status Codacy code quality Javadoc Maven Central

Java profiler

Getting SMBJ

To get SMBJ, you have two options:

  1. Add a dependency to SMBJ to your project.

  2. Build SMBJ yourself.

And, if you want, you can also run the SMBJ examples.

Binary releases of SMBJ are not provided here, but you can download it straight from the Maven Central repository if you want to.

Examples

A - Listing Files on a Share/Folder

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

B - Deleting a file

    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")) {
            share.rm("FILE");
        }
    }

C - Adjusting Timeout and Socket Timeout

    SmbConfig config = SmbConfig.builder()
            .withTimeout(120, TimeUnit.SECONDS) // Timeout sets Read, Write, and Transact timeouts (default is 60 seconds)
            .withSoTimeout(180, TimeUnit.SECONDS) // Socket Timeout (default is 0 seconds, blocks forever)
            .build();

    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")) {
            ...
        }
    }

Depending on SMBJ

If you’re building your project using Maven, you can add the following dependency to the pom.xml:

<dependency>
  <groupId>com.hierynomus</groupId>
  <artifactId>smbj</artifactId>
  <version>0.4.0</version>
</dependency>

If your project is built using another build tool that uses the Maven Central repository, translate this dependency into the format used by your build tool.

Building SMBJ

  1. Clone the SMBJ repository.

  2. Ensure you have Java7 installed with the Unlimited strength Java Cryptography Extensions (JCE).

  3. Run the command ./gradlew clean build.

About

Server Message Block (SMB2, SMB3) implementation in Java

License:Other


Languages

Language:Java 82.2%Language:Groovy 16.4%Language:XSLT 1.4%