springboot 集成 jcifs(samba)操作共享目录文件

一、工程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);

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值