SMB协议
SMB(Server Message Block)通信协议是微软(Microsoft)和英特尔(Intel)在1987年制定的协议,主要是作为Microsoft网络的通讯协议。SMB 是在会话层(session layer)和表示层(presentation layer)以及小部分应用层(application layer)的协议。
SMB使用了NetBIOS的应用程序接口 (Application Program Interface,简称API)。另外,它是一个开放性的协议,允许了协议扩展——使得它变得更大而且复杂;大约有65个最上层的作业,而每个作业都超过120个函数,甚至Windows NT也没有全部支持到,最近微软又把 SMB 改名为 CIFS(Common Internet File System),并且加入了许多新的特色。
示例
首先需要下载jar包jcifs-1.3.19,其中url地址的格式为:”smb://登录名:密码@IP地址/共享文件夹名称/”;
public static void getShareFile(){
String url = "smb://Administrator:aaaa11@10.1.241.69/FFOutput/";
try{
SmbFile file = new SmbFile(url);
if(file.exists()){
SmbFile[] files = file.listFiles();
for(SmbFile f : files){
System.out.println(f.getName());
String localDir = "E:";
InputStream in = new BufferedInputStream(new SmbFileInputStream(f));
File localFile = new File(localDir + File.separator + f.getName());
OutputStream out = new BufferedOutputStream(new FileOutputStream(localFile));
System.out.println(f.getContentLength());
byte[] buffer = new byte[1024*1024];
while(in.read(buffer) != 1){
out.write(buffer);
buffer = new byte[1024*1024];
}
in.close();
out.close();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
遇到的坑:
- url = “smb://Administrator:aaaa11@10.1.241.69/FFOutput/”; smb后面记得是://
- unkownHostException:可能IP地址不对或者如1的情况。
- Account is not known or password is bad。登录名称或者密码不对。
- User Account Restriction 密码不能为空。详见链接