0
点赞
收藏
分享

微信扫一扫

JAVA 远程共享文件处理

产品喵dandan米娜 2022-02-28 阅读 55

使用的是 org.samba.jcifs jar包

代码

import cn.hutool.core.io.IoUtil;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 远程共享文件处理
 * @Author dlw
 * @create 2022/2/28 14:10
 */
public class SmbUtil {


    /**
     *验证
     * @param account
     * @param pwd
     * @return
     */
    public static NtlmPasswordAuthentication getNtlmPasswordAuthentication(String account, String pwd){
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, account, pwd);
        return auth;
    }

    /**
     * 创建 文件夹
     * @param dirPath
     * @param auth
     */
    public static boolean mkdir(String dirPath, NtlmPasswordAuthentication auth){
        boolean resultFlag = true;
        // 域服务器验证
        SmbFile remoteFile = null;
        try {
            remoteFile = new SmbFile(dirPath, auth);
            if(!remoteFile.exists()){
                remoteFile.mkdir();
            }
        } catch (Exception e) {
            resultFlag = false;
            e.printStackTrace();
        }
        return resultFlag;
    }

    /**
     * 创建 文件
     * @param dirPath
     * @param auth
     */
    public static boolean createNewFile(String dirPath, NtlmPasswordAuthentication auth){
        boolean resultFlag = true;
        // 域服务器验证
        SmbFile remoteFile = null;
        try {
            remoteFile = new SmbFile(dirPath, auth);
            if(!remoteFile.exists()){
                remoteFile.createNewFile();
            }
        } catch (Exception e) {
            resultFlag = false;
            e.printStackTrace();
        }
        return resultFlag;
    }

    /**
     * 上传 文件
     * @param dirPath
     * @param ins
     * @param auth
     */
    public static void uploadFile(String dirPath, InputStream ins, NtlmPasswordAuthentication auth){
        // 域服务器验证
        SmbFile remoteFile = null;
        OutputStream out = null;
        try {
            remoteFile = new SmbFile(dirPath, auth);
            if(!remoteFile.exists()){
                remoteFile.createNewFile();
            }
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
            byte[] buffer = new byte[1024];
            int byteRead;
            while ((byteRead = ins.read(buffer)) != -1) {
                out.write(buffer, 0, byteRead);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            IoUtil.close(out);
            IoUtil.close(ins);
        }
    }

    /**
     * 下载 文件
     * @param dirPath
     * @param auth
     */
    public static InputStream downloadFile(String dirPath, NtlmPasswordAuthentication auth){
        // 域服务器验证
        SmbFile remoteFile = null;
        InputStream ins = null;
        try {
            remoteFile = new SmbFile(dirPath, auth);
            ins = new BufferedInputStream(new SmbFileInputStream(remoteFile));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ins;
    }
}
举报

相关推荐

0 条评论