Android 區域網路 - JCIFS Library

在Android 機器上想要讀取Windows 或 Linux 網路芳鄰中檔案時,

可使用第三方Library : Jcifs 來實現.

Jcifs 屬於 LGPL License, 只要不更改到 Jcifs 相關source code就不須將Souce Code 公開.

但Jcifs目前只支援smb 1,不支援smb 2/smb 3。若要支援smb 2/3 可使用

Step 1: 取得 Jcifs jar 檔案

可從官網 :https://jcifs.samba.org/ 下載 jar 檔.

將jar檔案複製到 /project_name/libs 中

Step 2: 設定 Library

在Project檔案列表中, 在libs/jcifs-1.3.19.jar按右鍵, 選擇 Add to Library

Step 3: 取得遠端檔案

  • 無使用者帳號密碼

    String remoteURL = “smb://192.168.0.155”;
    SmbFile dir = new SmbFile(remoteURL);

  • 需要使用者帳號密碼

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, userName, password);
    String remoteURL = “smb://192.168.0.155”;
    SmbFile dir = new SmbFile(remoteURL, auth);

Step 4: 下載遠端檔案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
String remoteURL = "smb://192.168.0.155";
String remoteFile = "//qtytr01publicnb//cmsfiles//test.csv";

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, userName, password);
SmbFile dir = new SmbFile(remoteURL + remoteFile, auth);
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();
作者

Nick Lin

發表於

2019-04-08

更新於

2023-01-18

許可協議


評論