栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一.前言

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

二.远程共享目录操作

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

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

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


 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、上传文件都远程共享目录


 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;
 }
 
 @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远程共享目录的操作代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/144619.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号