当前位置: 移动技术网 > IT编程>开发语言>Java > Java远程共享目录的操作代码

Java远程共享目录的操作代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

一.前言

     根据客户反馈,在进行文件下载的时候,新增远程共享目录,下载对应的文件到远程共享目录,采用常用的io操作模式,提示下载成功,但是客户去远程共享目录查看对应的下载文件,反馈说没有找到对应的文件。要求系统需要支持上传远程共享目录,为什么有一个这样的需求?由于下载的文件涉及到了支付文件,里面的金额不允许进行修改,如果放在本地路径有可能会不会出现人为的修改,一般涉及到钱的问题,客户都是比较谨慎的,刚好没有接触过操作远程共享目录的,就google了一下看有没有对应的操作说明,下面简单总结一下。

二.远程共享目录操作

1、需要下载对应的jcifs-1.3.18.jar,本例子采用3.18版本的,下载链接:

2、涉及的主要类是  smbfile(远程文件操作类) ,还有就是进行登录验证,验证对应的远程目录的合法性的操作,其他操作就普通的io流的操作。

3、从远程共享目录下载文件

/**
  * 方法说明:从远程共享目录下载文件
  * @param localdir   本地临时路径
  * @param removedir  远程共享路径
  * @param _filename  远程共享文件名
  * @param removeip   远程共享目录ip
  * @param removeloginuser 远程共享目录用户名
  * @param removeloginpass 远程共享目录密码
  * @return
  * @throws exception
  */
 public static int smbdownload(string localdir, string removedir,
   string _filename, string removeip, string removeloginuser,
   string removeloginpass) throws exception {
  inputstream in = null;
  outputstream out = null;
  try {
   ntlmpasswordauthentication auth = new ntlmpasswordauthentication(
     removeip, removeloginuser, removeloginpass);
   smbfile remotefile = new smbfile(removedir + _filename, auth);
   if (!remotefile.exists()) {
    return 0;
   }
   file dir = new file(localdir);
   if (!dir.exists()) {
    dir.mkdirs();
   }
   string filename = _filename.substring(_filename.lastindexof("\\")+1, _filename.length());
   file localfile = new file(localdir + filename);
   in = new bufferedinputstream(new smbfileinputstream(remotefile));
   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 {
    if (null != out) {
     out.close();
    }
   } catch (ioexception e) {
    e.printstacktrace();
   } finally {
    if (null != in) {
     try {
      in.close();
     } catch (ioexception e) {
      e.printstacktrace();
     }
    }
   }
  }
  return 1;
 }

4、上传文件都远程共享目录

/**
  * 方法说明:上传文件到远程共享目录
  * @param localdir   本地临时路径(a:/测试/测试.xls)
  * @param removedir  远程共享路径(smb://10.169.2.xx/测试/,特殊路径只能用/)
  * @param removeip   远程共享目录ip(10.169.2.xx)
  * @param removeloginuser 远程共享目录用户名(user)
  * @param removeloginpass 远程共享目录密码(password)
  * @return
  * @throws exception 0成功/-1失败
  */
 public static int smbuploading(string localdir, string removedir,
   string removeip, string removeloginuser, string removeloginpass) throws exception {
  ntlmpasswordauthentication auth = null;
  outputstream out = null;
  int retval = 0; 
  try {
   file dir = new file(localdir);
   if (!dir.exists()) {
    dir.mkdirs();
   }
   inetaddress ip = inetaddress.getbyname(removeip); 
   uniaddress address = new uniaddress(ip);
   // 权限验证
    auth = new ntlmpasswordauthentication(removeip, removeloginuser, removeloginpass);
   smbsession.logon(address,auth); 
   //远程路径判断文件文件路径是否合法
   smbfile remotefile = new smbfile(removedir + dir.getname(), auth);
   remotefile.connect();  
   if(remotefile.isdirectory()){ 
    retval = -1;
   }
   // 向远程共享目录写入文件
   out = new bufferedoutputstream(new smbfileoutputstream(remotefile));
   out.write(tobytearray(dir));
  } catch (unknownhostexception e) {
   retval = -1;
   e.printstacktrace();
  } catch (malformedurlexception e) {
   retval = -1;
   e.printstacktrace();
  } catch (smbexception e) {
   retval = -1;
   e.printstacktrace();
  } catch (ioexception e) {
   retval = -1;
   e.printstacktrace();
  } finally{
   if (out != null) {
    try {
     out.close();
    } catch (ioexception e) {
     e.printstacktrace();
    }
   }
  }
  return retval;
 }
 /**
  * mapped file way mappedbytebuffer 可以在处理大文件时,提升性能
  *
  * @param file 文件
  * @return 字节数组
  * @throws ioexception io异常信息
  */
 @suppresswarnings("resource")
 public static byte[] tobytearray(file file) throws ioexception {
  filechannel fc = null;
  try {
   fc = new randomaccessfile(file, "r").getchannel();
   mappedbytebuffer bytebuffer = fc.map(mapmode.read_only, 0,
     fc.size()).load();
   byte[] result = new byte[(int) fc.size()];
   if (bytebuffer.remaining() > 0) {
    bytebuffer.get(result, 0, bytebuffer.remaining());
   }
   return result;
  } catch (ioexception e) {
   e.printstacktrace();
   throw e;
  } finally {
   try {
    fc.close();
   } catch (ioexception e) {
    e.printstacktrace();
   }
  }
 }

总结

以上所述是小编给大家介绍的java远程共享目录的操作代码,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网