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();
}
稍後詳細整理,先記錄一下。