一、工程jar引用(引入相关依赖)
<!-- samba -->
<dependency>
<groupId>jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.17</version>
</dependency>
二、进行封装smb的操作
(1)初始化所需对象
将smb协议所需要进行使用到的参数进行封装成为对象,内容包括ip、用户名、密码、路径、文件名、输入流、输出流。
public class SMBConfig {
private String smbip;
private String smbuser;
private String smbps;
private String smbpath;
private String smbfile;
private InputStream is;
private OutputStream os;
public SMBConfig() {
}
public SMBConfig(String smbpath, String smbfile, InputStream is) {
this.smbip = "10.10.10.11";
this.smbuser = "test";
this.smbps = "test123";
this.smbpath = smbpath;
this.smbfile = smbfile;
this.is = is;
}
}
(2)封装smb的操作
将操作文件的上传、下载、删除封装成为对应的工具方法。
public class SMBUtils {
private static final Logger log = LogManager.getLogger(SMBUtils.class);
/**
* 往samba上传文件
* @param url 服务器IP地址
* @param userName 用户登录名
* @param password 用户登录密码
* @param storePath 服务器文件存储路径
* @param fileName 服务器文件存储名称
* @param is 文件输入流
* @return true:上传成功
*
* false:上传失败
* */
public static boolean storeFile(String url, String userName, String password, String storePath, String fileName, InputStream is) {
BufferedInputStream bf = null;
OutputStream os = null;
boolean result = false;
try {
UniAddress ua = UniAddress.getByName(url);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(url, userName, password);
SmbSession.logon(ua, auth);
SmbFile sf = new SmbFile("smb://" + url + storePath+"/"+fileName, auth);
SmbFile sffolder = new SmbFile("smb://" + url + storePath, auth);
if(!sffolder.exists())
sffolder.mkdirs();
bf = new BufferedInputStream(is);
os = sf.getOutputStream();
byte[] bt = new byte[1024];
int n = bf.read(bt);
while (n != -1){
os.write(bt, 0, n);
os.flush();
n = bf.read(bt);
}
// IOUtils.copy(is, sf.getOutputStream());
result = true;
} catch (Exception e) {
log.error(e.getMessage(),e);
}finally {
try {
if(bf!=null)
bf.close();
if(os!=null)
os.close();
} catch (IOException e) {
log.error(e.getMessage(),e);
}
}
return result;
}
/**
* 从samba下载文件
* @param url 服务器IP地址
* @param userName 用户登录名
* @param password 用户登录密码
* @param remotePath 服务器文件存储路径
* @param fileName 服务器文件存储名称
* @param os 文件输出流
* @return true:下载成功
*
* false:下载失败
* */
public static boolean retrieveFile(String url, String userName, String password, String remotePath, String fileName, OutputStream os) {
boolean result = false;
BufferedInputStream bf = null;
try {
UniAddress ua = UniAddress.getByName(url);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(url, userName, password);
SmbSession.logon(ua, auth);
SmbFile sf = new SmbFile("smb://"+ url + remotePath+"/"+fileName, auth);
// SmbFile sf = new SmbFile("smb://" +userName+":"+password+"@"+ url + remotePath+"/"+fileName, auth);
if(sf.exists()) {
bf = new BufferedInputStream(sf.getInputStream());
byte[] bt = new byte[1024];
int n = bf.read(bt);
while (n != -1){
os.write(bt, 0, n);
os.flush();
n = bf.read(bt);
}
// IOUtils.copy(sf.getInputStream(), os);
result = true;
}
} catch (Exception e) {
log.error(e.getMessage(),e);
}finally {
try {
if(bf!=null)
bf.close();
if(os!=null)
os.close();
} catch (IOException e) {
log.error(e.getMessage(),e);
}
}
return result;
}
/**
* 从samba删除文件
* @param url 服务器IP地址
* @param userName 用户登录名
* @param password 用户登录密码
* @param remotePath 服务器文件存储路径
* @param fileName 服务器文件存储名称
* @return true:删除成功
*
* false:删除失败
* */
public static boolean deleteFile(String url, String userName, String password, String remotePath, String fileName) {
boolean result = false;
try {
UniAddress ua = UniAddress.getByName(url);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(url, userName, password);
SmbSession.logon(ua, auth);
SmbFile sf = new SmbFile("smb://" + url + remotePath+"/"+fileName, auth);
sf.delete();
result = true;
} catch (Exception e) {
log.error(e.getMessage(),e);
}
return result;
}
/**
* 往samba上传文件
* @param SMBConfig
* @return
* */
public static boolean storeFile(SMBConfig smbConfig) {
return storeFile(smbConfig.getSmbip(), smbConfig.getSmbuser(), smbConfig.getSmbps(), smbConfig.getSmbpath(), smbConfig.getSmbfile(), smbConfig.getIs());
}
/**
* 从samba下载文件
* @param SMBConfig
* @return
* */
public static boolean retrieveFile(SMBConfig smbConfig) {
return retrieveFile(smbConfig.getSmbip(), smbConfig.getSmbuser(), smbConfig.getSmbps(), smbConfig.getSmbpath(), smbConfig.getSmbfile(), smbConfig.getOs());
}
}
(3)实际调用操作
//samba上传
SMBConfig config = new SMBConfig(dirPath, fileName, file.getInputStream());
boolean result = SMBUtils.storeFile(config);
//samba下载
SMBConfig config = new SMBConfig(dirPath, fileName, bouts);
boolean result = SMBUtils.retrieveFile(config);