java通过SMB协议读写共享文件

4 篇文章 0 订阅
2 篇文章 0 订阅

前段时间做的一个项目有用到SMB协议的,通过局域网访问共享文件夹,关于SMB协议网上很多讲解,这里我不做过多解释,我只要讲下我在使用SMB协议访问远程服务器共享文件的时候的一些经验:

   smb访问的基本格式:smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx

   注意:如果密码中有像“@”这种特殊字符的情况,就要通过

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.1.103", "Administrator", "19921103");

就行登录验证,具体实现如下:

 

package com.szitrus.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;

import org.apache.commons.io.IOUtils;

public class SMBUtils {
    /**
     * Description: 从共享目录拷贝文件到本地
     *
     * @Version1.0 Sep 25, 2009 3:48:38 PM
     * @param remoteUrl
     *          共享目录上的文件路径
     * @param localDir
     *          本地目录
     */
    public static byte[] smbGet(String remoteUrl, NtlmPasswordAuthentication auth) {
        byte[] bytes = {};
        InputStream is = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            SmbFile remoteFile = new SmbFile(remoteUrl, auth);
            if (remoteFile == null) {
                System.out.println("共享文件不存在");
                return null;
            }

            is = remoteFile.getInputStream();
            IOUtils.copy(is, baos);
            bytes = baos.toByteArray();
        } catch (Exception e) {
            logger.error("文件从共享目录下载失败", e);
        } finally {
            if (null != baos){
                try {
                    baos.close();
                } catch (IOException e) {
                    logger.error("文件从共享目录下载失败", e);
                }
            }

            if (null != is){
                try {
                    is.close();
                } catch (IOException e) {
                    logger.error("文件从共享目录下载失败", e);
                }
            }
        }

        return bytes;
    }


    /**
     * Description: 从本地上传文件到共享目录
     *
     * @Version1.0 Sep 25, 2009 3:49:00 PM
     * @param remoteUrl
     *          共享文件目录
     * @param localFilePath
     *          本地文件绝对路径
     */
    public static String smbPut(String remoteUrl, String localFilePath, NtlmPasswordAuthentication auth) {
        String result = null;
        FileInputStream fis = null;
        try {
            File localFile = new File(localFilePath);
            localFile.setReadOnly();
            String fileName = localFile.getName();
            SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName, auth);
            fis = new FileInputStream(localFile);
            IOUtils.copyLarge(fis, remoteFile.getOutputStream());
            result = "success";
        } catch (Exception e) {
            result = "failed";
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
              logger.error("文件从上传失败", e);
            }
        }
        return result;
    }

    /**
     * Description: 从共享目录下载文件
     *
     * @Version1.0 Sep 25, 2009 3:48:38 PM
     * @param remoteUrl
     *          共享目录上的文件路径
     */
    public static void smbDel(String remoteUrl, NtlmPasswordAuthentication auth) {
        try {
            SmbFile remoteFile = new SmbFile(remoteUrl, auth);
            if (remoteFile.exists()) {
                remoteFile.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        SMBUtils test = new SMBUtils();
        // smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx
        // test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt",
        // "c://") ;
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.1.103", "Administrator", "19921103"); // 先登录验证
        // SmbFile fp = new SmbFile(remoteurl+"//"+dir,auth);
        // test.smbPut("smb://10.12.91.156/smp_shared", "C:\\contract1659.pdf", auth);
        // test.smbGet("smb://10.12.91.156/smp_shared/contractvayiVyjK.pdf",
        // "C://asd.pdf", auth);
        String url = "smb://192.168.1.103/shared/12.txt";
        test.smbDel(url, auth);
    }
}

jcifs-1.3.17.jar

希望对大家有所帮助。以下是涉及到的主要的jar包:

 

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值