一、导入包(maven中央仓库https://mvnrepository.com/artifact/org.samba.jcifs/jcifs/1.3.3

<!-- jcifs 共享文件夹目录 -->
<dependency>
  <groupId>org.samba.jcifs</groupId>
  <artifactId>jcifs</artifactId>
  <version>1.3.3</version>
</dependency>

 二、工具类


import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

import java.io.*;

public class shareSmbFileUtil {
    private static String USER_DOMAIN = "ip";
    private static String USER_ACCOUNT = "username";
    private static String USER_PWS = "password";

    /**
     * @Title smbGet
     * @Param shareUrl 共享目录中的文件路径,如smb://192.168.1.158/share/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.println("共享文件不存在");
            return;
        }
        // 有文件的时候再初始化输入输出流
        InputStream in = null;
        OutputStream out = null;
        System.out.println("下载共享目录的文件 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 2019-01-10 20:16
     */
    public static void smbPut(String shareDirectory, String localFilePath,String userDomain,
                              String userAccount,String userPassword) {
        InputStream in = null;
        OutputStream out = null;
        try {
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(userDomain, userAccount,
                    userPassword);
            File localFile = new File(localFilePath);

            String fileName = localFile.getName();
            SmbFile remoteFile = new SmbFile(shareDirectory + "/" + fileName,auth);
            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();
            }
        }
    }

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

        // remoteUrl 格式示例 【smb://132.20.2.33/CIMPublicTest/】
        // 目录
        String shareDir = "smb://192.168.1.158/share/";
        String localDir = "D://ht/file/PDF/test.pdf";
        smbPut(shareDir,localDir,USER_DOMAIN,USER_ACCOUNT,USER_PWS);
//        listFiles(shareDir);

    }

    /**
     * @Title listFiles
     * @Description 遍历指定目录下的文件
     * @date 2019-01-11 09:56
     */
    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.println("远程共享目录访问耗时:【{}】" + (System.currentTimeMillis() - startTime));

        if (remoteFile.exists()) {
            SmbFile[] files = remoteFile.listFiles();
//            remoteFile.listFiles(shareDirectory);

            for (SmbFile f : files) {
                System.out.println(f.getName());
            }
        } else {
            System.out.println("文件不存在");
        }
    }
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐