利用SMB协议操作共享文件夹

利用SMB协议操作共享文件夹

SMB:服务器信息块,是一个网络文件共享协议。举一简单例子,在一个局域网内,有两台机器A,B,假设A共享了一个文件,那么可以在B上就可以使用SBM协议来操作这个文件夹。假如A机器有一个用户test,密码与是test,而共享的文件夹名称为电影,机器A的IP是192.168.0.102.代码如下:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

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


public class Main {

    private static String url = "smb://test:test@192.168.0.102/电影/";
    private static String moveUrl = "smb://test:test@192.168.0.102/电影/tt.dll";
    private static String targetUrl = "smb://test:test@192.168.0.102/电影/newdir/";


    /**创建目录*/
    public static void createDir(SmbFile parent)
    {
        try{
            if(parent != null && parent.isDirectory())
            {
                String path = parent.getPath() + "newdir/";
                SmbFile newFile = new SmbFile(path);
                if(newFile == null || !newFile.exists())
                {
                    newFile.mkdir();
                }
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    /**上传文件到服务器*/
    public static void uploadFile(File file)
    {
        BufferedInputStream bf = null; 
        SmbFileOutputStream smbOut = null;
        try{
            smbOut = new SmbFileOutputStream(url + "/" + file.getName(), false); 
            bf = new BufferedInputStream(new FileInputStream(file)); 
            byte[] bt = new byte[8192]; 
            int n = bf.read(bt); 
            while (n != -1){ 
                smbOut.write(bt, 0, n); 
                smbOut.flush(); 
                n = bf.read(bt); 
            } 
        }catch(Exception e) { 
            e.printStackTrace(); 
        }finally{
            try { 
                if(null != smbOut) 
                    smbOut.close(); 
                if(null != bf) 
                    bf.close(); 
            }catch(Exception e2) { 
                e2.printStackTrace(); 
            } 
        } 
    }

    /**移动文件到另一个目录*/
    public static void moveFile()
    {
        SmbFileOutputStream smbOut = null;
        BufferedInputStream bf = null;
        SmbFile moveFile = null;
        boolean flag = false;
        try{
            moveFile = new SmbFile(moveUrl);
            if(moveFile != null)
            {
                String name = moveFile.getName();
                smbOut = new SmbFileOutputStream(targetUrl + name, false);
                bf = new BufferedInputStream(new SmbFileInputStream(moveFile));
                 byte[] bt = new byte[8192]; 
                int n = bf.read(bt); 
                while (n != -1){ 
                    smbOut.write(bt, 0, n); 
                    smbOut.flush(); 
                    n = bf.read(bt); 
                } 
            }

            flag = true;
        }catch(Exception e)
        {
            e.printStackTrace();
        }finally{
            try { 
                if(null != smbOut) 
                    smbOut.close(); 
                if(null != bf) 
                    bf.close(); 
                if(moveFile != null && flag)
                    moveFile.delete();
            }catch(Exception e2) { 
                e2.printStackTrace(); 
            } 
        }
    }

    /**复制文件到另一个目录*/
    public static void copyFile()
    {
        SmbFileOutputStream smbOut = null;
        BufferedInputStream bf = null;
        SmbFile moveFile = null;
        try{
            moveFile = new SmbFile(moveUrl);
            if(moveFile != null)
            {
                String name = moveFile.getName();
                smbOut = new SmbFileOutputStream(targetUrl + name, false);
                bf = new BufferedInputStream(new SmbFileInputStream(moveFile));
                 byte[] bt = new byte[8192]; 
                int n = bf.read(bt); 
                while (n != -1){ 
                    smbOut.write(bt, 0, n); 
                    smbOut.flush(); 
                    n = bf.read(bt); 
                } 
            }

        }catch(Exception e)
        {
            e.printStackTrace();
        }finally{
            try { 
                if(null != smbOut) 
                    smbOut.close(); 
                if(null != bf) 
                    bf.close(); 
            }catch(Exception e2) { 
                e2.printStackTrace(); 
            } 
        }
    }

    /**文件重命名*/
    public static void rename()
    {
        try{
            SmbFile moveFile = new SmbFile(moveUrl);
            SmbFile targetFile = new SmbFile(moveFile.getParent() + "111111.dll/");
            moveFile.renameTo(targetFile);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }

    public static void main(String[] args)
    {
        try{
            SmbFile smbFile = new SmbFile(url);
            if(smbFile.isDirectory())
            {
                //创建一个文件夹
//              createDir(smbFile);

                //上传一个文件
//              uploadFile(new File("F://test//tt.dll"));

                //移动一个文件到另一目录
//              moveFile();

                //复制一个文件到另一个目录
//              copyFile();

                //重命名一个文件
                rename();

                SmbFile[] list = smbFile.listFiles();
                int length = list.length;
                for(int i = 0;i < length;i ++)
                {
                    System.out.println(list[i].getName() + " " + list[i].getParent());
                }
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

在这里我们将会使用到的一个额外的jar包,为jcifs-1.1.11.jar。该jar可以由网上下载
下载地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值