Android Network Neighborhood - Jcifs-ng

Uploading or downloading file at local network between Android and Windows by using smb socket.

Android app can use Jcifs library to access file which is in the Windows system. But Jcifs only support smb 1.

If Windows system only support smb2 or smb3, we must use other library, like smbj or jcifs-ng.

Because smbj library just for Android SDK Platform 26 (Android O, Android 8), so this article used jcifs-ng to implement.

Step 1: Import Jcifs -ng

In Jcifs-ng website,add as follow in build.gradle

build.gradle
1
2
3
4
5
<dependency>
<groupId>eu.agno3.jcifs</groupId>
<artifactId>jcifs-ng</artifactId>
<version>2.1.2</version>
</dependency>

also change to

build.gradle
1
2
3
4
5
6
7
8
android {
...
}

dependencies {
...
compile 'eu.agno3.jcifs:jcifs-ng:2.1.2'
}

Step 2: Access Remote File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
String userName = "user";
String password = "1234";
String remoteFile = "/files/test.csv";
String remoteURL = "smb://nickpublicpc";

CIFSContext baseCxt = new BaseContext(new PropertyConfiguration(System.getProperties()));
NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator(userName, password);
CIFSContext ct = baseCxt.withCredentials(auth);
SmbFile smbFile = new SmbFile(remoteURL + remoteFile, ct);

SmbFileInputStream inputSmbFileStream = new SmbFileInputStream(smbFile);

File localFile = new File(filePath);
FileOutputStream outputFileStream = new FileOutputStream(localFile);

byte[] buffer = new byte[4096];
int length = 0;
while ((length = inputSmbFileStream.read(buffer)) > 0) {
outputFileStream.write(buffer, 0, length);
}

outputFileStream.close();
inputSmbFileStream.close();

As mentioned above, we can access file at the local network, but Jcifs-ng support smb3 partially.

Author

Nick Lin

Posted on

2019-04-09

Updated on

2023-01-18

Licensed under


Comments