首页 > 系统相关 >SpringBoot访问windows共享文件

SpringBoot访问windows共享文件

时间:2023-02-05 15:56:11浏览次数:55  
标签:SpringBoot filePath windows ip shareFolder localDir new 共享 String

前言

最近有项目需要开发档案打包下载功能,其中包含很多大附件,项目使用minio存储且不在同一台服务器上,为了优化速度决定使用windows共享功能进行文件传输

SMB1.0

集成jcifs类库,主要适用于一些老旧系统,但下载速度比较慢,仅作参考

此类库没有maven引用,官网地址:http://jcifs.samba.org/

注意事项:

设置jcifs.smb.client.dfs.disabled选项开启,可以提高传输速度

使用NtlmPasswordAuthentication认证代替smb协议url携带用户名密码方式,避免特殊字符传递造成认证失败

 public static void downloadFile(String ip, String shareFolder, String filePath, String localDir) throws Exception {
        System.setProperty("jcifs.smb.client.dfs.disabled", "true");
        String url = getFileUrl(ip, shareFolder, filePath);
        SmbFile smbFile = new SmbFile(url);
        smbFile.connect();
        FileUtil.initfloderPath(localDir);
        String localFilePath = localDir + "/" + smbFile.getName();
        BufferedInputStream buf = new BufferedInputStream(new SmbFileInputStream(smbFile));
        FileUtil.writeFile(localFilePath, FileUtil.convertStreamToByte(buf));
    }

    public static void downloadFileByAuth(String ip, String shareFolder, String userName, String password, String filePath, String localDir) throws Exception {
        System.setProperty("jcifs.smb.client.dfs.disabled", "true");
        String url = getFileUrl(ip, shareFolder, filePath);
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, userName, password);
        SmbFile smbFile = new SmbFile(url, auth);
        smbFile.connect();
        FileUtil.initfloderPath(localDir);
        String localFilePath = localDir + "/" + smbFile.getName();
        BufferedInputStream buf = new BufferedInputStream(new SmbFileInputStream(smbFile));
        FileUtil.writeFile(localFilePath, FileUtil.convertStreamToByte(buf));
    }
    
    public static String getFileUrl(String ip, String shareFolder, String filePath) {
        return "smb://" + ip + "/" + shareFolder + "/" + filePath;
    }

SMB2.0

集成smbj类库,适用于windows server2012及以上操作系统,默认安装开启无需额外配置

此类库maven引用很久没有发布最新版本,需要下载代码自行编译,github地址:https://github.com/hierynomus/smbj

经测试,500MB文件传输大概比minio协议传输快了4秒左右,小文件传输速度基本保持一致

  public static void downloadFileV2(String ip, String shareFolder, String filePath, String localDir) throws Exception {
        SMBClient client = new SMBClient(SmbConfig.createDefaultConfig());
        Connection conn = client.connect(ip);
        Session session = conn.authenticate(AuthenticationContext.anonymous());
        downLoadSMB2(session, shareFolder, filePath, localDir);
    }

    public static void downloadFileByAuthV2(String ip, String shareFolder, String userName, String password, String filePath, String localDir) throws Exception {
        SMBClient client = new SMBClient(SmbConfig.createDefaultConfig());
        Connection conn = client.connect(ip);
        Session session = conn.authenticate(new AuthenticationContext(userName, password.toCharArray(), ip));
        downLoadSMB2(session, shareFolder, filePath, localDir);
    }

    private static void downLoadSMB2(Session session, String shareFolder, String filePath, String localDir) throws Exception {
        InputStream fis = null;
        FileOutputStream os = null;
        DiskShare diskShare = null;
        try {
            diskShare = (DiskShare) session.connectShare(shareFolder);
            if (!diskShare.fileExists(filePath)) {
                throw new FileNotFoundException(filePath);
            }
            if (!diskShare.isConnected())
                diskShare = (DiskShare) session.connectShare(shareFolder);

            com.hierynomus.smbj.share.File file = diskShare.openFile(filePath,
                    EnumSet.of(AccessMask.GENERIC_READ),
                    (Set) null,
                    SMB2ShareAccess.ALL,
                    SMB2CreateDisposition.FILE_OPEN,
                    (Set) null
            );
            fis = file.getInputStream();
            FileUtil.initfloderPath(localDir);
            String[] filePathList = filePath.split("\\/");
            String localFilePath = localDir + "/" + filePathList[filePathList.length - 1];
            os = new FileOutputStream(localFilePath);
            byte[] b = new byte[4096];
            int length;
            while ((length = fis.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            IOUtils.close(os);
            IOUtils.close(fis);
            if (diskShare != null && diskShare.isConnected()) diskShare.close();
        }
    }

445端口被禁用解决办法

一般企业/政府项目为了系统安全会禁用445端口,而445端口禁用后文件共享功能无法使用,此时我们需要进行端口转发,即将客户端445端口转发到共享服务器端口A,共享服务器将本地端口A转发到445即可完成共享,具体操作步骤如下,192.168.1.164就是共享文件服务器的内网ip

查看服务器转发规则

netsh interface portproxy show all

删除服务器转发规则

netsh interface portproxy reset

共享文件服务器

  • 执行CMD代码
netsh interface portproxy add v4tov4 listenport=4455 listenaddress=192.168.1.164 connectport=445 connectaddress=127.0.0.1
netsh interface portproxy add v4tov4 listenport=4455 listenaddress=127.0.0.1 connectport=445 connectaddress=127.0.0.1

客户端服务器

  • 关闭Server服务
  • CMD执行代码
netsh interface portproxy add v4tov4 listenaddress=127.0.0.1 listenport=445 connectaddress=192.168.1.164 connectport=4455
  • 重启系统

标签:SpringBoot,filePath,windows,ip,shareFolder,localDir,new,共享,String
From: https://www.cnblogs.com/yanpeng19940119/p/17093476.html

相关文章

  • 安装Windows Server 2022 - 初学者系列 - 学习者系列文章
          这天要写一个关于系统部署的系列文章,涉及到WindowsServer2022操作系统的安装,所以就写了此文。Windows系列的操作系统安装,以前的博文中都有介绍,这里再次做一......
  • Windows 下配置Kubectl
    下载kubectl.exe二进制文件使用curl.exe-LO"​​https://dl.k8s.io/release/v1.26.0/bin/windows/amd64/kubectl.exe​​"命令下载kubectl.exe二进制文件,如果担心下......
  • qt实现类似windows的屏幕保护(小球碰撞动画)
    最近看了篇文章啊,讲的就是让小球进行移动,可能别人做的是仿真啊,用到了太多的数学函数,什么运动学,各种的,我就想着,自己能不能使用qt实现下这种效果,就是有一个球不停的移动,当碰......
  • windows右键菜单扩展容器[开源]
    今天给大家分享一个我做的小工具,可以自定义扩展右键菜单的功能来提高工作效率,效果图如下:image如上图,右键菜单多了几个我自定义的菜单复制文件路径复制文件夹路......
  • 如何重装Windows系统——以Windows10为例
    写在重装前重装前注意备份系统盘(一般是C盘)中的数据你需要一个U盘可以把操作系统看做成一个软件软件运行的时候无法删除软件一般情况下系统盘是C盘步骤重装系统主......
  • SpringBoot测试用例的一些小技巧~
    场景一:不想因为测试而对数据库产生脏数据@TestpublicvoidtestInsert(){ Useruser=newUser(); user.setUsername("startqbb"); user.setPassword("123456"); u......
  • SpringBoot和Vue整合ECharts
    一、Vue安装EChartsnpmiecharts-S二、Vue整合ECharts其实这个很简单首先在vue中引入ECharts 2.然后我们直接去ECharts官网使用一些图形的代码,放到Home.vue中,所......
  • windows下修改键位
    过程如下:搜索框输入cmd进入命令行输入regedit命令找到[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\KeyboardLayout]新建一个scancodemap的文件(右键新建......
  • Windows系统密钥(实测可用)
    WindowsServer2003R2sp2standardedition文件名:cn_win_srv_2003_r2_standard_with_sp2_cd1_X13-13927.iso链接:ed2k://|file|cn_win_srv_2003_r2_standard_with_sp2_......
  • SpringBoot整合JDBC详解
    SpringBoot整合JDBC@​​TOC​​前言对于数据访问层,无论是关系型数据库(SQL)还是NOSQL(非关系型数据库),SpringBoot的底层都是采用SpringData的方式来进行统一处理。SpringData其......