smbfile远程操作共享文件并解决连接数不足的问题

本文介绍如何使用Spring Integration框架结合JCIFS库实现对远程SMB共享文件的操作,包括文件的移动、复制及删除。通过示例代码展示了如何建立SMB会话,遍历目录下所有文件,并将它们移动到备份目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错信息:No more connections can be made to this remote computer at this time because there are already as many connections as the computer can accept.

准备jar包(以gradle为例):

compile group: 'org.springframework.integration', name: 'spring-integration-smb', version: '1.0.0.RELEASE'
compile group: 'org.springframework.integration', name: 'spring-integration-file', version: '5.0.4.RELEASE'
compile group: 'org.springframework.integration', name: 'spring-integration-stream', version: '5.0.4.RELEASE'
compile group: 'org.codelibs', name: 'jcifs', version: '1.3.18.3'

以移动远程电脑//192.168.6.133/share文件下的文件为例子:

public void goTest() {
        SmbSession session=null;
        String ip = "192.168.6.133";
        String directory = "share";
        String userName="lzc";
        String pwd = "1";
        String rootURL = "//"+ ip +"/"+ directory +"/";
        /*Config.setProperty("jcifs.smb.client.soTimeout", "100000");//*/
        try {
            SmbShare share=new SmbShare("smb://" + userName +":"+ pwd +"@"+ ip +"/share/");
            session=new SmbSession(share);
            String backUp=rootURL +"backUp/";
            if(!session.exists(backUp)){
                session.mkdir(backUp);
            }
            for (String name : share.list()){
                System.out.println(name);
                SmbFile smbForm=session.createSmbFileObject(rootURL + name);
                if(smbForm.isDirectory()){
                    continue;
                }
                SmbFile smbBackUp = session.createSmbDirectoryObject(backUp + name);
                smbForm.copyTo(smbBackUp);
                smbForm.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            session.close();
        }
        
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值