Android利用SMB协议,查看文件或者下载文件,以及播放SMB服务器视频
1.说明:假设现在手机需要再局域网下,下载电脑上的文件或者同局域网内其他存储设备上的文件,(存储设备有SMBA服务)。
2.用到的资源
JCIFS项目: https://jcifs.samba.org (我目前使用的是jcifs-1.3.19.jar版本,放在andorid项目中的lib包下)
SMB错误码: https://msdn.microsoft.com/en-us/library/ee441884.aspx
3.准备工作完毕现在开始正式code
3.1 先获取相对应的权限
//验证是否许可权限
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
String[] permissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE};
//验证是否许可权限
for (String str : permissions) {
if (getActivity().checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申请权限
getActivity().requestPermissions(permissions, REQUEST_CODE_CONTACT);
return;
} else {
//这里就是权限打开之后自己要操作的逻辑
responseListener(); //该方法就是开启线程去读取SMB上的文件
}
}
}
3.2 开启线程去读取和下载文件哦
private void responseListener() {
showLoading();
new Thread() {
@Override
public void run() {
super.run();
//获取模糊图
try {
ip = (String) SharePreferenceUtil.get(getActivity(), SharePreferenceUtil.Current_IP, "");
String path = "smb://" + ip + "/ImageData/Images/" + CaseDetailMsgActivity.getCurrentID() + "/thumb/";
initSmb(path);
if (mRootFolder.exists()) {
SmbFile[] smbFiles = mRootFolder.listFiles(); // smb://192.168.128.146/ImageData/Images/4033/thumb/
for (int i = 0; i < smbFiles.length; i++) {
//smb电脑上共享的文件夹路径是Images/用户ID/thumb/图片.jpg
//比如Images/4025/thumb/zhangsan.jpg
//所以我把相对于的照片,存在SD卡上新建MyData/Images/对应用户ID/thumb/zhangsan.jpg的目录项,每一个照片都是对号入座的
File toFile = new File(Environment.getExternalStorageDirectory() +
"/MyData/Images/" + CaseDetailMsgActivity.getCurrentID() + "/thumb/");
if (!toFile.exists()) { //不存在创建,(说明我下载过了,SD卡存过了) toFile.mkdirs();
String remoteUrl = "smb://cmeftproot:lzjdzh19861207@" + ip + "/";
SDFileUtil.downLoadFileToFolder(remoteUrl, "ImageData/Images/" + CaseDetailMsgActivity.getCurrentID() + "/thumb/",
smbFiles[i].getName(), toFile.getAbsolutePath());
} else {
//存在,在判断smb文件数量和本地文件数量,不同就同步文件
if (toFile.listFiles().length != smbFiles.length) {
String remoteUrl = "smb://cmeftproot:lzjdzh19861207@" + ip + "/";
SDFileUtil.downLoadFileToFolder(remoteUrl, "ImageData/Images/" + CaseDetailMsgActivity.getCurrentID() + "/thumb/",
smbFiles[i].getName(), toFile.getAbsolutePath());
}
}
}
mHandler.sendEmptyMessage(REFRESH);
} else {
mHandler.sendEmptyMessage(EMPTY);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
3.3这个是初始化SMB链接
private void initSmb(String rootPath) throws UnknownHostException, SmbException, MalformedURLException {
ip = (String) SharePreferenceUtil.get(getActivity(), SharePreferenceUtil.Current_IP, ""); //请填写你的ip
System.setProperty("jcifs.smb.client.dfs.disabled", "true"); //true false
System.setProperty("jcifs.smb.client.soTimeout", "1000000");
System.setProperty("jcifs.smb.client.responseTimeout", "30000");
String username = "cmeftproot"; //请填写你的用户名
String password = "lzjdzh19861207"; //请填写你的密码
UniAddress mDomain = UniAddress.getByName(ip);
NtlmPasswordAuthentication mAuthentication = new NtlmPasswordAuthentication(ip, username, password);
SmbSession.logon(mDomain, mAuthentication);
mRootFolder = new SmbFile(rootPath, mAuthentication);
}
3.4这个是读取SMB设备的IO流下载方法
/**
* 已下是SMB文件读取到本地SD卡的下载工具
*==========================================================================================
*/
/**
* 下载文件到指定文件夹
*
* @param remoteUrl
* @param shareFolderPath
* @param fileName
* @param localDir
*/
public static void downLoadFileToFolder(String remoteUrl, String shareFolderPath, String fileName,
String localDir) {
try {
SmbFile remoteFile = new SmbFile(remoteUrl + shareFolderPath + fileName);
File localFile = new File(localDir + "/" + fileName);
BufferedInputStream in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[8 * 1024];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
到此OK了!!
二:播放SMB服务器视频
请直接下载Demo,在里面播放传入需要读取视频文件的路径即可,我的播放器是VLC,能实现录屏,截图等等功能,这里就不累赘了!!
我贴一个Demo吧
Demo下载链接,我本地是OK的哦,如果不行请再三检查你自己的代码!!!!
https://download.csdn.net/download/tongzhengtong/13976842