JAVA远程访问共享目录

php中文网
发布: 2016-06-07 17:45:38
原创
1183人浏览过

 1.2 jcifs

  Jcifs pan>是一个用JAVA开发的SMB客户端库,利用jcifs可以操作windows共享文件,可以得到域用户,实现单点登录,最新版本为:1.3.12,官方网址:http://jcifs.samba.org/

  2. 代码实现

  package uploadSMB;

  import java.io.BufferedInputStream;

立即学习Java免费学习笔记(深入)”;

  import java.io.BufferedOutputStream;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileOutputStream;

  import java.io.IOException;

  import java.io.InputStream;

  import java.io.OutputStream;

  import jcifs.smb.SmbFile;

  import jcifs.smb.SmbFileInputStream;

  import jcifs.smb.SmbFileOutputStream;

  public class UploadDownloadUtil {

  /**

  * Description: 从共享目录拷贝文件到本地

  * @Version1.0 Sep 25, 2009 3:48:38 PM

  * @param remoteUrl 共享目录上的文件路径

  * @param localDir 本地目录

  */

  public 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();

  File localFile = new File(localDir+File.separator+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 {

  out.close();

  in.close();

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  }

  /**

  * Description: 从本地上传文件到共享目录

  * @Version1.0 Sep 25, 2009 3:49:00 PM

  * @param remoteUrl 共享文件目录

  * @param localFilePath 本地文件绝对路径

  */

  public void smbPut(String remoteUrl,String localFilePath) {

  InputStream in = null;

  OutputStream out = null;

  try {

  File localFile = new File(localFilePath);

  String fileName = localFile.getName();

  SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);

  in = new BufferedInputStream(new FileInputStream(localFile));

  out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));

  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();

  }

  }

  }

  public static void main(String[] args){

  UploadDownloadUtil test = new UploadDownloadUtil() ;

  // smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx

  //test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;

  test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;

  }

  }

  2.3 remoteUrl说明

  remoteUrl如何填写是值得注意的

  如果是无需密码的共享,则类似如下格式:

  smb://ip/sharefolder(例如:smb://192.168.0.77/test)

  如果需要用户名、密码,则类似如下格式:

  Smb://username:password@ip/sharefolder(例如:smb://chb:123456@192.168.0.1/test)

  // smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx

  //test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;

  test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号