• java读取局域网共享文件夹中文件并保存到本地文件夹


    在磁盘新建一个文件夹,右击文件夹属性,点击共享
    在这里插入图片描述

    点击网络和共享中心
    在这里插入图片描述

    设置文件夹可访问权限

    在这里插入图片描述
    在这里插入图片描述

    到此就可以用本地ip加文件夹名称访问了,同局域网也可以通过改地址访问
    在这里插入图片描述
    文件夹的名称来自这张图的 网络路径
    在这里插入图片描述
    如果需要通过java的jcifs包访问请继续

    这一步开启SMB1和服务器协议配置

    命令查看是否开启

    Get-SmbServerConfiguration | Select EnableSMB1Protocol, EnableSMB2Protocol
    
    • 1

    在这里插入图片描述
    可以看出当前服务器SMB1未开启,尝试开启SMB1,

    powershell控制台输入:

    Set-SmbServerConfiguration -EnableSMB1Protocol $true
    
    • 1

    报错,指定服务不存在,所以当前的服务器不支持SMB1,到此,原因找到了,因为本地使用的机器支持SMB1所以可以正常运行jcifs的功能,但是正式环境的服务器不支持SMB1,所以报错
    我试了几台电脑都报错,所以只能通过控制面板开启,如下图:点击确定后需要重启生效
    在这里插入图片描述

    java项目 maven 引入

            
            
                jcifs
                jcifs
                1.3.17
            
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    以下代码来自他人博客,略作修改,读取和下载文件是可用的,其他未测试

    import jcifs.smb.SmbException;
    import jcifs.smb.SmbFile;
    import jcifs.smb.SmbFileInputStream;
    import jcifs.smb.SmbFileOutputStream;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    import java.net.MalformedURLException;
    import java.net.URLEncoder;
    import java.net.UnknownHostException;
    
    /**
     * java 通过 SmbFile 类操作共享文件夹
     */
    public class SmbFileUtil {
    
        /**
         * 读取共享文件夹下的所有文件(文件夹)的名称
         *
         * @param remoteUrl
         */
        public static SmbFile[] getSharedFileList(String remoteUrl) {
            SmbFile smbFile;
            try {
                // smb://userName:passWord@host/path/
                smbFile = new SmbFile(remoteUrl);
                if (!smbFile.exists()) {
                    System.out.println("no such folder");
                } else {
                    SmbFile[] files = smbFile.listFiles();
    //                for (SmbFile f : files) {
    //                    System.out.println(f.getName() + f.getDate());
    //                }
                    return files;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SmbException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    
        /**
         * 创建文件夹
         *
         * @param remoteUrl
         * @param folderName
         * @return
         */
        public static void smbMkDir(String remoteUrl, String folderName) {
            SmbFile smbFile;
            try {
                // smb://userName:passWord@host/path/folderName
                smbFile = new SmbFile(remoteUrl + folderName);
                if (!smbFile.exists()) {
                    smbFile.mkdir();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SmbException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 上传文件
         *
         * @param remoteUrl
         * @param shareFolderPath
         * @param localFilePath
         * @param fileName
         */
        public static void uploadFileToSharedFolder(String remoteUrl, String shareFolderPath, String localFilePath, String fileName) {
            InputStream inputStream = null;
            OutputStream outputStream = null;
            try {
                File localFile = new File(localFilePath);
                inputStream = new FileInputStream(localFile);
                // smb://userName:passWord@host/path/shareFolderPath/fileName
                SmbFile smbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName);
                smbFile.connect();
                outputStream = new SmbFileOutputStream(smbFile);
                byte[] buffer = new byte[4096];
                int len = 0; // 读取长度
                while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
                    outputStream.write(buffer, 0, len);
                }
                // 刷新缓冲的输出流
                outputStream.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    outputStream.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 下载文件到浏览器
         *
         * @param httpServletResponse
         * @param remoteUrl
         * @param shareFolderPath
         * @param fileName
         */
        public static void downloadFileToBrowser(HttpServletResponse httpServletResponse, String remoteUrl , String fileName) {
            SmbFile smbFile;
            SmbFileInputStream smbFileInputStream = null;
            OutputStream outputStream = null;
            try {
                // smb://userName:passWord@host/path/shareFolderPath/fileName
                smbFile = new SmbFile(remoteUrl  + "/" + fileName);
                smbFileInputStream = new SmbFileInputStream(smbFile);
                httpServletResponse.setHeader("content-type", "application/octet-stream");
                httpServletResponse.setContentType("application/vnd.ms-excel;charset=UTF-8");
                httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + fileName);
                // 处理空格转为加号的问题
                httpServletResponse.setHeader("Content-Disposition", "attachment; fileName=" + fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"));
                outputStream = httpServletResponse.getOutputStream();
                byte[] buff = new byte[2048];
                int len;
                while ((len = smbFileInputStream.read(buff)) != -1) {
                    outputStream.write(buff, 0, len);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SmbException e) {
                e.printStackTrace();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    outputStream.close();
                    smbFileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    
        /**
         * 下载文件到指定文件夹
         *
         * @param remoteUrl
         * @param shareFolderPath
         * @param fileName
         * @param localDir
         */
        public static void downloadFileToFolder(String remoteUrl, String fileName, String localDir) {
            InputStream in = null;
            OutputStream out = null;
            try {
                SmbFile remoteFile = new SmbFile(remoteUrl  + "/" + fileName);
                File localFile = new File(localDir + File.separator + fileName);
                in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
                out = new BufferedOutputStream(new FileOutputStream(localFile));
                byte[] buffer = new byte[1024];
                while (in.read(buffer) != -1) {
                    out.write(buffer);
                    buffer = new byte[1024];
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    
        /**
         * 删除文件
         *
         * @param remoteUrl
         * @param shareFolderPath
         * @param fileName
         */
        public static void deleteFile(String remoteUrl, String shareFolderPath, String fileName) {
            SmbFile SmbFile;
            try {
                // smb://userName:passWord@host/path/shareFolderPath/fileName
                SmbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName);
                if (SmbFile.exists()) {
                    SmbFile.delete();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SmbException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209

    业务实现代码
    FileUtil.isFile来自cn.hutool.core.io (hutool包),判断文件是否存在的
    upLoadPath 是本地地址的文件夹路径

    代码意思就是读取共享文件夹的文件,然后下载保存到本地地址

     @GetMapping(value = "/getSmbFile")
        public Result getSmbFile(HttpServletResponse httpServletResponse) {
            SmbFile[] files = SmbFileUtil.getSharedFileList("smb://10.10.41.53/可选性曲线图片/");
            SmbFile smbFile = files[files.length - 1];
            
            boolean bo = FileUtil.isFile(new File(upLoadPath+"\\"+smbFile.getName()));
            if (!bo){
                SmbFileUtil.downloadFileToFolder("smb://10.10.41.53/可选性曲线图片/", smbFile.getName(), upLoadPath);
            }
            return Result.ok(smbFile.getName());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    C++保姆级入门教程(9)—— 一维数组基础
    莫队
    Java多线程中——部分场景使用实现
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java宿舍管理系统8n6jf
    51单片机STC89C52RC——9.1 DS1302涓流充电计时芯片
    Linux进程详解三:进程状态
    第十五章 配置工作队列管理器类别
    解决了个bug,想说点啥但又难以启齿
    文盘Rust -- 给程序加个日志
    数据结构与算法_二叉搜索树
  • 原文地址:https://blog.csdn.net/qq_35226176/article/details/127911772