美文网首页
Java windows共享文件操作类 smb

Java windows共享文件操作类 smb

作者: 咖啡机an | 来源:发表于2018-12-14 10:02 被阅读0次
注:1.若用户是管理员(域中)可能会导致无法正常读取文件。建议[新建一个账户](https://www.jianshu.com/p/407aa3069d83)试一试
2.如果密码带有#,smb用以下方式无法正确解析,查看源码会发现#是关键字,最终解析的密码不会带上#,可以使用
NtlmPasswordAuthentication au = new NtlmPasswordAuthentication(user:pwd);
SmbFile remoteFile = new SmbFile(fromPath,au);
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;

/**
 * Windows共享文件操作工具类
 * 
 *
 * @author archie
 * @date 2018-11-19
 */
public class SmbFileUtil {

    /**
     * 共享文件夹配置
     */
    private static final String HOST = "";
    private static final String USER_NAME = "";
    private static final String PASSWORD = "";
    private static final String PATH = "";
    private static final String REMOTE_URL = "smb://" + USER_NAME + ":" + PASSWORD + "@" + HOST + PATH;

    /**
     * 下载文件
     *
     * @param fileName 文件名 + .文件类型 1.txt
     * @param localDir 文件保存文件夹
     * @throws IOException
     */
    public static String smbGet(String fileName, String localDir) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        StringBuffer result = new StringBuffer();
        try {
            SmbFile remoteFile = new SmbFile(REMOTE_URL + fileName);
            if (remoteFile == null || !remoteFile.exists()) {
                //文件不存在
                return "";
            }
            result.append(localDir).append(File.separator).append(fileName);
            File localFile = new File(result.toString());
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            byte[] buffer = new byte[1024];
            int bytesToRead = -1;
            while ((bytesToRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
        return result.toString();
    }

    /**
     * 上传文件
     *
     * @param localFilePath 文件路径 + 文件名 + .文件类型
     * @throws IOException
     */
    public static void smbPut(String localFilePath) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            File localFile = new File(localFilePath);
            String fileName = localFile.getName();
            SmbFile remoteFile = new SmbFile(REMOTE_URL + fileName);
            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
            byte[] buffer = new byte[1024];
            int bytesToRead = -1;
            while ((bytesToRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * 远程文件删除 文件名 + .文件类型
     *
     * @param fileName
     */
    public static void smbDelete(String fileName) {
        SmbFile file = null;
        try {
            file = new SmbFile(REMOTE_URL + fileName);
            if (file.exists()) {
                file.delete();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (SmbException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        //smbGet("105TA919-4.jpg","e:");
         smbPut("E:\\scooper\\pom.xml");
        //smbDelete("1.txt");
    }
}

相关文章

  • Java windows共享文件操作类 smb

  • samba服务配置

    samba是一个实现类UNIX操作系统与WINDOWS操作系统之间共享的一种SMB协议的软件。包括的文件共享和打印...

  • samba服务

    smb(samba): 1、用于linux和windows之间的文件共享,可以实现匿名用户和本地用户之间的文件共享...

  • Liunx虚拟机与Wiindows共享文件夹创建

    Linux与windows共享文件夹构建 samba 1.介绍 Samba是在Linux和Windows利用SMB...

  • 横向移动之smb中继攻击

    背景知识 什么是SMB服务器消息块(SMB)协议是一种网络文件共享协议,在Microsoft Windows中实现...

  • Mac 访问 windows(win10)共享文件

    【windows端】 1. 开启SMB文件共享支持服务 1-1)所有设置中搜索“启用或关闭windows功能” 1...

  • T2. samba

    samba用于ubuntu与windows文件共享的工具,在ubuntu中下载samba,配置smb.conf,在...

  • centos7搭建samba

    下载 创建共享目录 配置文件 /etc/samba smb.conf 创建登录用户 启动 windows添加

  • SMB协议操作共享文件

    在 java 开发中,避免不了要对共享文件进行操作,前段时间小编就做了类似的需求,这里记录开发中遇到的坑,一是记录...

  • Samba配置文件

    1.smb.conf文件 /etc/samba/smb.conf这个文件大概分为全局配置和共享配置 smb.son...

网友评论

      本文标题:Java windows共享文件操作类 smb

      本文链接:https://www.haomeiwen.com/subject/vxywhqtx.html