服务器文件上传和下载

SmbFile 类实现局域网文件共享读写

 

1.开启远程服务器共享

2. maven添加依赖(如果不是maven工程,请下载jar包   jcifs.jar)

               

 <dependency>

          <groupId>jcifs</groupId>

           <artifactId>jcifs</artifactId>

            <version>1.3.17</version>

 </dependency>

3.  上传,下载,删除

 

String  path = "smb://root:mypassword@192.168.70.55/images/"

以下前提都是使用了jar包。jcifs.jar。并且远程服务器支持文件共享。

3.1  上传本地文件流到Samba服务器指定目录(字节) 

/**
     * 上传本地文件流到Samba服务器指定目录
     * @param url   文件上传的路径
     * @param bytes   文件字节
     * @param fileName  文件名
     */

 public static void uploadFileToSambaByBtyes(String url, byte[] bytes ,String fileName) throws IOException {
           url+=fileName;
        for (int i = 0; i < bytes.length; ++i) {
            if (bytes[i] < 0) {//调整异常数据
                bytes[i] += 256;
            }
        }
        //检查远程父路径,不存在则创建
        SmbFile remoteSmbFile = new SmbFile(url);
        String parent = remoteSmbFile.getParent();
        SmbFile parentSmbFile = new SmbFile(parent);
        if (!parentSmbFile.exists()) {
            parentSmbFile.mkdirs();
        }
        OutputStream out = null;
        try{
            //打开一个远程Samba文件输出流,作为复制到的目的地
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteSmbFile));
            out.write(bytes);
        } catch  (Exception e) {
            e.printStackTrace();
        } finally  {
            try  {
                out.close();
            } catch  (IOException e) {
                e.printStackTrace();
            }
        }
    }

3.2上传本地文件  (文件类型)

 /**
     * 上传本地文件流到Samba服务器指定目录
     * @param url
     * @param fileFlow
     * @throws MalformedURLException
     * @throws SmbException
     */
    public static void uploadFileFlowToSamba(String url, String fileFlow,String fileName) throws IOException {
        if ((fileFlow == null) || ("".equals(fileFlow.trim()))) {
            System.out.println("文件路径流不可以为空");
            return;
        }
        url+=fileName;
        BASE64Decoder decoder = new BASE64Decoder();
        //Base64解码
        byte[] imageByte = decoder.decodeBuffer(fileFlow);
        for (int i = 0; i < imageByte.length; ++i) {
            if (imageByte[i] < 0) {//调整异常数据
                imageByte[i] += 256;
            }
        }
        //检查远程父路径,不存在则创建
        SmbFile remoteSmbFile = new SmbFile(url);
        String parent = remoteSmbFile.getParent();
        SmbFile parentSmbFile = new SmbFile(parent);
        if (!parentSmbFile.exists()) {
            parentSmbFile.mkdirs();
        }
        OutputStream out = null;
        try{
            //打开一个远程Samba文件输出流,作为复制到的目的地
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteSmbFile));
            out.write(imageByte);
        } catch  (Exception e) {
            e.printStackTrace();
        } finally  {
            try  {
                out.close();
            } catch  (IOException e) {
                e.printStackTrace();
            }
        }
    }

3.3  从samba服务器上下载指定的文件到本地目录  

/**
     * 从samba服务器上下载指定的文件到本地目录
     * @param remoteSmbFile   Samba服务器远程文件
     * @param localDir        本地目录的路径
     * @throws SmbException
     */

    public static void downloadFileFromSamba(SmbFile remoteSmbFile, String localDir) throws SmbException {

        if (!remoteSmbFile.exists()) {
            System.out.println("Samba服务器远程文件不存在");
            return;
        }
        if((localDir == null) || ("".equals(localDir.trim()))) {
            System.out.println("本地目录路径不可以为空");
            return;
        }

        InputStream in = null;
        OutputStream out = null;

        try{
            //获取远程文件的文件名,这个的目的是为了在本地的目录下创建一个同名文件
            String remoteSmbFileName = remoteSmbFile.getName();

            //本地文件由本地目录,路径分隔符,文件名拼接而成
            File localFile = new File(localDir + File.separator + remoteSmbFileName);

            // 如果路径不存在,则创建
            File parentFile = localFile.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }

            //打开文件输入流,指向远程的smb服务器上的文件,特别注意,这里流包装器包装了SmbFileInputStream
            in = new BufferedInputStream(new SmbFileInputStream(remoteSmbFile));
            //打开文件输出流,指向新创建的本地文件,作为最终复制到的目的地
            out = new BufferedOutputStream(new FileOutputStream(localFile));

            //缓冲内存
            byte[] buffer = new byte[1024];
            while (in.read(buffer) != -1){
                out.write(buffer);
                buffer = new byte[1024];
            }

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try  {
                out.close();
                in.close();
            } catch  (IOException e) {
                e.printStackTrace();
            }
        }
    }


3.5 删除服务器文件                     

 

  //删除服务器文件
                        SmbFile dfile = new SmbFile(path + sdHomeFileInfo.getFileName());
                        //判断是否存在,存在则删除
                        if (dfile.exists()) {
                            dfile.delete();
                        }

欢迎交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值