java 访问windows共享文件夹

背景

公司的ERP系统是由.net开发的,文件也是存放在windows服务器上,所以需要从window服务器上抓取相应的文件.

使用技术

Java可以使用JCIFS框架对Windows共享文件夹进行读写,就这个框架可以让我们像访问本地文件夹一下访问远程文件夹。

JCIFS框架是什么?

JCIFS是使用纯Java开发的一个开源框架,通过smb协议访问远程文件夹。该框架同时支持Windows共享文件夹和Linux共享文件夹,不过,Linux共享文件夹需要安装Samba服务软件(官网:http://www.samba.org/).

什么是smb协议?

SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。

代码书写

依赖导入

<dependency>
   <groupId>org.samba.jcifs</groupId>
    <artifactId>jcifs</artifactId>
    <version>1.3.3</version>
</dependency>

测试代码

public class SmbFileUtil {

    private static String USER_DOMAIN = "";  //域账号,没有可以不填
    private static String USER_ACCOUNT = "myaccount";  //账号
    private static String USER_PWS = "myaccount";  //密码

    public static void main(String[] args) throws Exception {

        String shareDir = "smb://10.66.52.72/文管资料/test.pdf";
        //listFiles(shareDir);
        smbGet(shareDir, "d://print");
    }

    /**
     * @Title listFiles
     * @Description 遍历指定目录下的文件
     * @date 2020-03-19
     */
    private static void listFiles(String shareDirectory) throws Exception {
        long startTime = System.currentTimeMillis();

        // 域服务器验证
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT, USER_PWS);
        SmbFile remoteFile = new SmbFile(shareDirectory, auth);
        System.out.print("远程共享目录访问耗时:【{}】" + (System.currentTimeMillis() - startTime));
        if (remoteFile.exists()) {
            SmbFile[] files = remoteFile.listFiles();
            //remoteFile.listFiles(shareDirectory);
            System.out.println("文件存在");
            for (SmbFile f : files) {
                System.out.println(f.getName());
            }
        } else {
            System.out.print("文件不存在");
        }
    }

    /**
     * @Title smbGet
     * @Param shareUrl 共享目录中的文件路径,如smb://135.20.21.112/myprint/test.txt
     * @Param localDirectory 本地目录,如tempStore/smb
     */
    public static void smbGet(String shareUrl, String localDirectory) throws Exception {
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT,
                USER_PWS);
        SmbFile remoteFile = new SmbFile(shareUrl, auth);
        if (!remoteFile.exists()) {
            System.out.print("共享文件不存在");
            return;
        }
        // 有文件的时候再初始化输入输出流
        InputStream in = null;
        OutputStream out = null;
        System.out.print("下载共享目录的文件 shareUrl 到 localDirectory");
        try {
            String fileName = remoteFile.getName();
            File localFile = new File(localDirectory + File.separator + fileName);
            File fileParent = localFile.getParentFile();
            if (null != fileParent && !fileParent.exists()) {
                fileParent.mkdirs();
            }
            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];
            }
            out.flush(); //刷新缓冲区输出流
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
            in.close();
        }
    }

    /**
     * @Title smbPut
     * @Description 向共享目录上传文件
     * @Param shareDirectory 共享目录
     * @Param localFilePath 本地目录中的文件路径
     * @date 2020-03-19
     */
    public static void smbPut(String shareDirectory, String localFilePath) {
        InputStream in = null;
        OutputStream out = null;
        try {
            File localFile = new File(localFilePath);
            String fileName = localFile.getName();
            SmbFile remoteFile = new SmbFile(shareDirectory + "/" + fileName);
            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
            byte[] buffer = new byte[1024];
            while (in.read(buffer) != -1) {
                out.write(buffer);
                buffer = new byte[1024];
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

各位看官,记得长按点赞三连哦!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值