Android手機(jī)SMB訪(fǎng)問(wèn)macOS文件

1準(zhǔn)備工作:

(1)mac【系統(tǒng)偏好】->【用戶(hù)與群組】->【用戶(hù)與群組】->點(diǎn)左下角解鎖 “+” 新建一個(gè)用戶(hù)"share"(賬戶(hù)類(lèi)型僅限共享,名稱(chēng)密碼必填,自己隨便命名)
(2)mac【系統(tǒng)偏好】->【共享】->勾選 【文件共享】(使用SMB來(lái)共享文件和文件夾),切換到【用戶(hù)】點(diǎn)“+”選擇剛剛新建的用戶(hù)"share"

2 AndroidStudio:

(1)app目錄下 build.gradle 下添加 implementation 'com.hierynomus:smbj:0.10.0'
(2)異步線(xiàn)程使用:

  val client = SMBClient()
  val connection = client.connect("192.168.0.xxx")
  val ac = AuthenticationContext("share", "share000".toCharArray(), "192.168.0.xxx")
  val session: Session = connection.authenticate(ac)
  val share = session.connectShare("共享的文件夾") as DiskShare?
  share?.list("子文件夾")?.forEach {
      Log.e("Tag","smb ->  ${it.fileName}")
      }
  //下載文件
  val filePath="共享的文件夾/子文件夾/xxx.txt"
  val smbFileRead = share.openFile(filePath, EnumSet.of(AccessMask.GENERIC_READ), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null)
  val input = smbFileRead.inputStream
  val outFile = File(Environment.getExternalStorageDirectory().absolutePath, "temp.txt")
  val out = FileOutputStream(outFile)
        try {
            // Transfer bytes from in to out
            val buf = ByteArray(4096)
            var len = input.read(buf)
            while (len > 0) {
                out.write(buf, 0, len)
                len = input.read(buf)
            }
            input.close()
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            out.close()
        }

3注意的地方:

(1)不要用guest賬戶(hù),不要用anonymous賬戶(hù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容