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

报错信息: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();
        }
        
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 Linux 系统下,可以通过 SmbFile 类库来访问 Windows 共享文件夹。SmbFile 是 JCIFS 库的一部分,它提供了一种简单的方式来访问 SMB/CIFS 协议的文件共享。 以下是一个示例代码,演示如何通过 SmbFile 访问 Windows 共享文件夹: ```java import jcifs.smb.SmbFile; public class SmbExample { public static void main(String[] args) { String username = "your_username"; // Windows 用户名 String password = "your_password"; // Windows 密码 String sharedFolder = "smb://192.168.0.100/shared_folder/"; // 共享文件夹路径 String filename = "example.txt"; // 文件名 try { // 创建 SmbFile 对象 SmbFile smbFile = new SmbFile(sharedFolder + filename, username, password); // 读取文件内容 byte[] buffer = new byte[4096]; int bytesRead; try (InputStream inputStream = smbFile.getInputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } System.out.println(outputStream.toString()); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先创建了一个 SmbFile 对象,然后通过 getInputStream() 方法获取文件的输入流,并读取文件内容。注意,在访问共享文件夹时需要提供 Windows 用户名和密码。 此外,还可以使用 SmbFile 的其他方法来获取文件属性、创建文件夹、上传文件等操作。更多详细信息可以参考 JCIFS 官方文档。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值