龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > Oracle 技术 >

JAVA远程访问共享目录

时间:2011-03-14 23:24来源:未知 作者:admin 点击:
分享到:
1.2 jcifs Jcifs script src=http://hi.images.csdn.net教程/js/blog/tiny_mce/themes/advanced/langs/zh.js type=text/javascript教程/scriptscript src=http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js type=text/javascript

 1.2 jcifs

  Jcifs <script src="http://hi.images.csdn.net教程/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript教程"></script><script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> pan>是一个用JAVA开发的SMB客户端库,利用jcifs可以操作windows共享文件,可以得到域用户,实现单点登录,最新版本为:1.3.12,官方网址:http://jcifs.samba.org/

  2. 代码实现

  package uploadSMB;

  import java.io.BufferedInputStream;

  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];

  }
精彩图集

赞助商链接