SMB共享文件夹操作

最近接到了要获取其他服务器上文件操作的需求,使用了网络共享文件夹的形式来进行处理。

 

首先在服务器上设置一个文件夹为共享文件夹,这里使用了盘符G:

之后在本地我的电脑下邮件生成一个网络位置,输入ip所在:例子:\\172.17.16.16\G,取一个别名,例子为g;

之后即可通过这个目录操作远程的文件夹,记得设置权限为完全控制。

贴一下主要的两个工具类的方法:

package com.neusoft.jkysjbs.action.comm;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;

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

public class SmbFileTransfer {

  /* 
   * smb类 用于对共享文件夹的文件进行上传下载使用,直接使用file类不能获取到相关的文件对象
   * 
   */ 
    
    
    /**
     * 将本地文件上传到共享文件夹中
     */
     public static void smbUpload(String localDir,String remoteUrl) {
       InputStream in = null;
       OutputStream out = null;
       try {
           //获取图片
           File localFile = new File(localDir);
           SmbFile remoteFile = new SmbFile(remoteUrl + "/"  + localFile.getName());
           remoteFile.connect(); //尝试连接

           in = new BufferedInputStream(new FileInputStream(localFile));
           out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));

           byte[] buffer = new byte[4096];
           int len = 0; //读取长度
           while ((len = in.read(buffer, 0, buffer.length)) != -1) {
               out.write(buffer, 0, len);
           }
           out.flush(); //刷新缓冲的输出流
       }
       catch (Exception e) {
           String msg = "发生错误:" + e.getLocalizedMessage();
           System.out.println(msg);
       }
       finally {
           try {
               if(out != null) {
                   out.close();
               }
               if(in != null) {
                   in.close();
               }
           }
           catch (Exception e) {}

           
       }
   } 
 
        
     /**
      * 将文件从共享文件夹中下载下来到本地
      * @param remoteUrl
      * @param localDir
      */
        public static void smbGet(String remoteUrl, String localDir) {
            InputStream in = null;
            OutputStream out = null;
            try {
            SmbFile remoteFile = new SmbFile(remoteUrl);
            if (remoteFile == null) {
            System.out.println("共享文件不存在");
            return;
            }
            String fileName = remoteFile.getName();
            //创建文件夹(根目录下直接创建文件可以直接new File,如果是多层目录下文件的创建,需要先创建多层文件夹,然后用输入流在文件夹相应目录下再将文件建立)
            File localFile = new File(localDir + File.separator);
            if(!localFile.exists()){//如果文件夹不存在
                //创建文件夹,mkdir是创建根目录,mkdirs创建多层目录
                localFile.mkdirs();
            }
            //创建文件
            BufferedWriter bw=new BufferedWriter(new FileWriter(localDir + File.separator + fileName));
            bw.close();
            File localFiles =  new File(localDir + File.separator + fileName);
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new BufferedOutputStream(new FileOutputStream(localFiles));
            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 (Exception e) {
            e.printStackTrace();
            }
            }
            }
        
        
        public static void main(String[] args) {
               //格式 smb://共享服务器的用户:密码@ip/共享文件夹别名/其下目录
            smbGet("smb://username:password@172.17.16.16/g/ftp/test.zip","E:/test/");
            smbUpload("E:/test.zip","smb://username:password@172.17.16.16/g/ftp/");
        }
        
   
}

个人的思考是:SmbFile应该不是File的一个子类,很多方法相似,但并没有继承关系

而且应该有更简便快捷的处理办法,如果有人了解请在评论指教,多谢。

另外类似于在指定url下生成,压缩文件这种操作,使用smbFile做不到,多数用法也是上传下载我的处理办法是在本地生成好--传上去--删除本地,不得不说很粗糙,如果哪位大神有好办法也请多指教,感谢阅读。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值