设为首页 加入收藏

TOP

SpringBoot访问windows共享文件(一)
2023-07-26 08:16:20 】 浏览:74
Tags:SpringBoot 访问 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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Eureka注册中心 下一篇day02-SpringMVC映射请求数据

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目