1. 程式人生 > >Java中SMB的相關應用

Java中SMB的相關應用

目錄

  • SMB 服務操作
    • Ⅰ SMB簡介
    • Ⅱ SMB配置
      • 2.1 Windows SMB
    • Ⅲ 新增SMB依賴
    • Ⅳ 路徑格式
    • Ⅴ 操作共享
    • Ⅵ 登入驗證

SMB 服務操作

Ⅰ SMB簡介

​ SMB(全稱是Server Message Block)是一個協議名,它能被用於Web連線和客戶端與伺服器之間的資訊溝通。SMB協議作為一種區域網檔案共享傳輸協議,常被用來作為共享檔案安全傳輸研究的平臺。
​ Windows作業系統都包括了客戶機和伺服器 SMB協議支援。Microsoft 為 Internet 提供了SMB的開源版本,即通用Internet檔案系統CIFS。與現有 Internet 應用程式如檔案傳輸協議FTP相比, CIFS 靈活性更大。對於UNIX系統,可使用一種稱為Samba的共享軟體。

Ⅱ SMB配置

2.1 Windows SMB

2.1.1 配置服務

​ 在本地機上以Windows10舉例 :在控制面板 -->程式-->程式和功能-->啟用或關閉Windows功能-->SMB 1.0/cifs file sharing support勾選SMB 1.0/CIFS ClientSMB 1.0/CIFS Server

2.1.2 驗證服務

​ 開啟之後來驗證一下SMB是否正確開啟:在DOS命令視窗用PowerShell命令進入程式輸入Get-SmbServerConfiguration | Select EnableSMB1Protocol, EnableSMB2Protocol

檢視服務狀態,如圖所示:

2.1.3 共享檔案

​ 在D盤新建一個測試檔案D:\Test\SmbTest\GoalTest右鍵選單-->授予訪問許可權-->特定使用者選擇一個使用者進行授權,如圖所示:

​ 授權給使用者之後會提示你的資料夾已共享,在DOS視窗輸入彈窗提示的共享連線\\DESKTOP-D5DVINV\Test即可進入共享資料夾,右擊共享資料夾還可以設定訪問密碼,更改訪問使用者等等。

Ⅲ 新增SMB依賴

​ 在pom.xml中新增SMB服務相關的依賴:

<!-- 引用SmbFile類的jar包  -->
<dependency>
       <groupId>jcifs</groupId>
       <artifactId>jcifs</artifactId>
       <version>1.3.17</version>
</dependency>

Ⅳ 路徑格式

​ 在Java中SMB路徑請求格式有如下三種情況:

  • 如果是無需密碼的共享,格式類似:smb://ip/sharefolder(例如:smb://192.168.0.77/test)
  • 如果需要使用者名稱和密碼,格式類似:smb://username:password@ip/sharefolder(例:smb://chb:[email protected]/test)
  • 如果使用者名稱密碼和域名,格式類似:smb:域名;使用者名稱:密碼@目的IP/資料夾/檔名.xxx(例:smb://orcl;wangjp:[email protected]/Test)

Ⅴ 操作共享

​ 以上步驟之後,就完成了在Windows上建立了一個SMB檔案伺服器和必要準備工作,接下來就是簡單的程式碼環節,上傳和下載的邏輯也比較簡單,對SMB共享檔案的操作其實就是處理SmbFile物件。

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import org.springframework.util.FileCopyUtils;
import java.io.*;
/**
 * @author: Create By WangJP
 * @description: SMB服務操作相關
 * @date: 2020/1/1
 */
public class Demo {

    private static final String SMB_SHARE_FOLDER = "smb://username:[email protected]/Test/";
    private static final String SHARE_FOLDER_PATH = "SmbTest\\GoalTest";
    private static final String FILE_NAME = "test.txt";
    private static final String LOCAL_DIR = "D:\\LocalTest";

    public static void main(String[] args) {
        downloadSmbFile(SMB_SHARE_FOLDER, SHARE_FOLDER_PATH, FILE_NAME, LOCAL_DIR);
        uploadFile(SMB_SHARE_FOLDER, SHARE_FOLDER_PATH, FILE_NAME, LOCAL_DIR);
    }

    /**
     * 從SMB共享資料夾下載檔案到本地
     * @param remoteUrl       SMB請求路徑Url
     * @param shareFolderPath 共享資料夾中SMB目標檔案存放的完整路徑
     * @param fileName        檔名
     * @param localDir        本地資料夾
     */
    public static void downloadSmbFile(String remoteUrl, String shareFolderPath, String fileName, String localDir) {
        InputStream in = null;
        OutputStream out = null;
        try {
            SmbFile smbfile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName);
            File localFile = new File(localDir + File.separator + fileName);
            in = new BufferedInputStream(new SmbFileInputStream(smbfile));
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            FileCopyUtils.copy(in, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeStreanm(in, out);
        }
    }

    /**
     * 將本地資料夾中的檔案上傳到SMB共享資料夾(與下載類似)
     * @param remoteUrl       SMB請求路徑Url
     * @param shareFolderPath 共享資料夾中SMB目標檔案存放的完整路徑
     * @param fileName        檔名
     * @param localDir        本地資料夾
     */
    private static void uploadFile(String remoteUrl, String shareFolderPath, String fileName, String localDir) {
        InputStream in = null;
        OutputStream out = null;
        try {
            SmbFile smbfile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName);
            File localFile = new File(localDir + File.separator + fileName);
            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(smbfile));
            FileCopyUtils.copy(in, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeStreanm(in, out);
        }
    }

    private static void closeStreanm(InputStream in, OutputStream out) {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

​ 自己工作中有一個業務需求是要檢測SMB共享目錄中的某個檔案是否存在,通過下載上傳的例子,學習到獲取 SmbFile 物件需要特定的的屬性(url canon等等)構建,處理方法上有很多和File物件類似,程式碼示例如下:

 /**
     * 檢驗SMB共享檔案是否存在
     * @param remoteUrl       SMB請求路徑Url
     * @param shareFolderPath 共享資料夾中SMB目標檔案存放的完整路徑
     * @param fileName        檔名
     * @return true:存在 false:不存在
     */
    public static boolean checkSmbFile(String remoteUrl, String shareFolderPath, String fileName) {
        boolean result = false;
        try {
            SmbFile smbfile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName);
            result = smbfile.exists();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

Ⅵ 登入驗證

​ SMB的登入驗證主要是為解決賬號密碼中存在特殊字元的問題(比如轉義字元,連結裡的特定字元),存在特殊字元的賬號密碼往往會報出下列異常:

Connected to the target VM, address: '127.0.0.1:54593', transport: 'socket'
jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.

​ 這時為了構建合法的SmbFile物件,我們就需要先進行登入驗證,再去嘗試構建該物件:

private static String domainip = "192.168.170.13";
private static String username = "username";
private static String password = "password";
private static String remoteurl = "smb://192.168.170.13/share";
//進行賬號IP地址登入驗證
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domainip, username, password);  
SmbFile smbfile = new SmbFile(remoteurl+"//"+folderpath,auth);