当前位置: 首页 > 工具软件 > jCIFS > 使用案例 >

Java基于jcifs包实现SMB协议连接linux服务器进行文件上传与下载操作源码

卫才
2023-12-01

添加依赖

// https://mvnrepository.com/artifact/jcifs/jcifs
    compile group: 'jcifs', name: 'jcifs', version: '1.3.17'

代码示例

package com.xl;

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

import java.io.*;

/**
 * 这个工具类提供了从Samba服务器上下载文件到本地目录,以及上传本地文件到Samba服务器指定目录的方法
 */
public class SambaTransferFile {

    public static void main(String[] args) {
        uploadFileToSamba();
        //downLoadFileFromSamba();
    }

    //上传本地文件到Samba服务器指定目录
    public static void uploadFileToSamba() {
        //smbFileUrl路径smb://smbuser:1234@192.168.0.101/smb,这样会报错,因为smb是共享目录,若不是共享目录则不会报错
        //String smbFileUrl = "smb://smb用户名:密码@IP地址/共享目录/";
        String smbFileUrl = "smb://smbuser:1234@192.168.0.101/smb/";
        String localDir = "F:\\look\\look.txt";
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            //创建一个本地文件对象
            File localFile = new File(localDir);
            //获取本地文件的文件名,此名字用于在远程的Samba服务器上指定目录创建同名文件
            String localFileName = localFile.getName();
            //创建远程服务器上Samba文件对象
            SmbFile remoteSmbFile = new SmbFile(smbFileUrl + File.separator + localFileName);
            //打开一个文件输入流执行本地文件,要从它读取内容
            inputStream = new BufferedInputStream(new FileInputStream(localFile));
            //打开一个远程Samba文件输出流,将文件复制到的目的地
            outputStream = new BufferedOutputStream(new SmbFileOutputStream(remoteSmbFile));

            //缓冲内存
            byte[] buffer = new byte[1024];
            while (inputStream.read(buffer) != -1) {
                outputStream.write(buffer);
            }
            System.out.println("文件上传完成!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //从samba服务器上下载指定的文件到本地目录
    public static void downLoadFileFromSamba() {
        //String smbFileUrl = "smb://smb用户名:密码@IP地址/共享目录/";
        String smbFileUrl = "smb://smbuser:1234@192.168.0.101/smb/look.txt";
        String localDir = "F:\\look";
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            //创建一个smbFile对象对应远程服务器上的SmbFile
            SmbFile smbFile = new SmbFile(smbFileUrl);
            //获取远程文件的文件名,这个的目的是为了在本地的目录下创建一个同名文件
            String smbFileName = smbFile.getName();
            //本地文件又本地目录,路径分隔符,文件名拼接而成
            File localFile = new File(localDir + File.separator + smbFileName);
            //打开文件输入流,指向远程的smb服务器上的文件,特别注意,这里流包装器包装了SmbFileInputStream
            inputStream = new BufferedInputStream(new SmbFileInputStream(smbFile));
            //打开文件输出流,指向新创建的本地文件,作为最终复制到的目的地
            outputStream = new BufferedOutputStream(new FileOutputStream(localFile));

            //缓冲内存
            byte[] buffer = new byte[1024];
            while (inputStream.read(buffer) != -1) {
                outputStream.write(buffer);
            }
            System.out.println("文件下载完成!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
 类似资料: