1.简介
要实现文件上传下载基于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)。
2.java实现方式共享文件夹操作
1.首先确保已设置好共享文件夹,局域网测试共享文件夹是否测试成功。网上有很多教程,这里不再叙述。
举例:现有A机(192.168.0.133)和B机(192.168.0.134),在同一局域网下,B机已设置共享文件夹。在A机首先windows+R键,调用命令窗口,输入\\192.168.0.134确保能访问到共享文件夹说明已设置成功,如不成功,请先设置共享文件夹。
2.需要用到jcifs-1.3.18.jar这个jar包。maven引用如下:
<dependency>
<groupId>jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.18</version>
</dependency>
3.SubUtil实现类:
public class SmbUtil {
/* 文件上传
* String localFilePath:要上传的本地文件路径
* String path:远程服务器共享文件夹名称
* String username:远程服务器用户名
* String password:远程服务器密码
* */
public static void upload(String localFilePath,String path,String username,String password) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
// jcifs.Config.setProperty("jcifs.smb.lmCompatibility","2");
String remoteUrl = "smb://192.168.0.134" + path + (path.endsWith("/") ? "" : "/");
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + localFile.getName(),auth);
remoteFile.connect();
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[4096];
int len = 0;
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
}
catch (Exception e) {}
}
}
/* 文件下载
* String localFilePath:本地文件夹
* String path:远程服务器共享文件名
* String username:远程服务器用户名
* String password:远程服务器密码
* */
public static void download(String localFilePath,String path, String username,String password){
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
// jcifs.Config.setProperty("jcifs.smb.lmCompatibility","2");
String remoteUrl = "smb://192.168.0.134" + path + (path.endsWith("/") ? "" : "/");
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + localFile.getName(),auth);
remoteFile.connect();
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[4096];
int len = 0;
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
}
catch (Exception e) {}
}
}
}
3.出现的异常
java.net.UnKnownHostException:..__MSBROWSE__..:
首先确保户号和密码完全正确。
错误写法:(用户名和密码不要拼接在remoteUrl里面。)
String remoteUrl = "smb://admin:admin@192.168.0.134" + path + "a.txt";
SmbFile remoteFile = new SmbFile(remoteUrl);
正确写法:
String username="admin";
String password="admin";
String remoteUrl = "smb:/192.168.0.134" + path + "a.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
SmbFile remoteFile = new SmbFile(remoteUrl);