Android 代碼讀取PC端的共享文件夾(一)

Android 通過代碼讀取PC端共享文件夾
準備:
1 android端:SMB/CIFS協議 百度百科
2.PC端:(1)設置一個共享文件夾,權限是所有人
在這裏插入圖片描述
在這裏插入圖片描述
(2)要關閉防火牆!!! 要關閉防火牆!!! 要關閉防火牆!!!

開發:
Android端:
(1)MyApp

 	    System.setProperty("jcifs.smb.client.dfs.disabled", "true");//默認false
        System.setProperty("jcifs.smb.client.soTimeout", "1000000");//超時
        System.setProperty("jcifs.smb.client.responseTimeout", "30000");//超時

(2) MainActivity
這個是耗時操作,所以要異步請求,這裏開了一個子線程
在這裏插入圖片描述

new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String ip = "192.168.31.200";//pc地址
                        String username = "administrator";//賬戶密碼
                        String password = "123456";
                        UniAddress mDomain = null;
                        try {
                        	//登錄授權
                            mDomain = UniAddress.getByName(ip);
                            NtlmPasswordAuthentication mAuthentication = new NtlmPasswordAuthentication(ip, username, password);
                            SmbSession.logon(mDomain, mAuthentication);
                            //登錄授權結束
                            String rootPath = "smb://" + ip + "/";
                            SmbFile mRootFolder;
                            try {
                                mRootFolder = new SmbFile(rootPath, mAuthentication);
                                try {
                                    SmbFile[] files;
                                    files = mRootFolder.listFiles();
                                    for (SmbFile smbfile : files) {
                                        Log.e("文件名稱----",smbfile.getCanonicalPath());//這個就能獲取到共享文件夾了
                                    }
                                } catch (SmbException e) {
                                    // ...
                                }
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            }

                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        } catch (SmbException e) {
                            e.printStackTrace();
                        }

稍後詳細整理,先記錄一下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章