SpringBoot访问windows共享文件的方法

 更新时间:2023年02月06日 08:39:01   作者:code2roc  
这篇文章主要介绍了SpringBoot访问windows共享文件,项目使用minio存储且不在同一台服务器上,为了优化速度决定使用windows共享功能进行文件传输,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下

前言

最近有项目需要开发档案打包下载功能,其中包含很多大附件,项目使用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访问windows共享文件的文章就介绍到这了,更多相关SpringBoot访问windows共享文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java数据结构之图(动力节点Java学院整理)

    Java数据结构之图(动力节点Java学院整理)

    本文章主要讲解学习如何使用JAVA语言以邻接表的方式实现了数据结构---图(Graph)。对java数据结构之图相关知识感兴趣的朋友一起学习吧
    2017-04-04
  • 使用javafx更新UI的方法

    使用javafx更新UI的方法

    这篇文章主要介绍了使用javafx更新UI的方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • SpringBoot实现接口防刷的五种方案

    SpringBoot实现接口防刷的五种方案

    接口防刷是保障系统安全与稳定性的重要措施,恶意的高频请求不仅会消耗服务器资源,还可能导致数据异常,甚至系统瘫痪,本文将介绍在SpringBoot框架下实现接口防刷的5种技术方案,需要的朋友可以参考下
    2025-04-04
  • SpringBoot快速入门详解

    SpringBoot快速入门详解

    springboot提供了一种快速使用spring的方式,基于coc的思想,即约定大于配置,通过本文学习快速掌握springboot入门知识,感兴趣的朋友跟随小编一起看看吧
    2021-07-07
  • Springboot单体架构http请求转换https请求来支持微信小程序调用接口

    Springboot单体架构http请求转换https请求来支持微信小程序调用接口

    这篇文章主要介绍了Springboot单体架构http请求转换https请求来支持微信小程序调用接口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Springboot中分析SQL性能的两种方式详解

    Springboot中分析SQL性能的两种方式详解

    文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环境,但高版本已弃用;p6spy框架功能更强大,可以自定义日志输出格式,但在生产环境中使用需谨慎,感兴趣的朋友一起看看吧
    2025-02-02
  • SpringBoot接入轻量级分布式日志框架(GrayLog)的操作方法

    SpringBoot接入轻量级分布式日志框架(GrayLog)的操作方法

    这篇文章主要介绍了SpringBoot接入轻量级分布式日志框架(GrayLog)的方法,本文通过图文实例相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • Spring Boot 内置工具类ReflectionUtils的实现

    Spring Boot 内置工具类ReflectionUtils的实现

    ReflectionUtils是一个反射工具类,它封装了Java反射的操作,使得我们能够更轻松地操作和访问类的方法、字段,本文主要介绍了Spring Boot 内置工具类ReflectionUtils的实现,感兴趣的可以了解一下
    2023-11-11
  • 使用Java手搓一个控制台进度条打印工具

    使用Java手搓一个控制台进度条打印工具

    这篇文章主要为大家详细介绍了如何使用Java手搓一个控制台进度条打印工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-04-04
  • 基于Jenkins搭建.NET Core持续集成环境过程图解

    基于Jenkins搭建.NET Core持续集成环境过程图解

    这篇文章主要介绍了基于Jenkins搭建.NET Core持续集成环境过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08

最新评论