码迷,mamicode.com
首页 > 编程语言 > 详细

Java读写Windows共享文件夹 .

时间:2016-03-18 21:50:18      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片、文件等。那么如何使用Java读写Windows共享文件夹呢?

Java可以使用JCIFS框架对Windows共享文件夹进行读写,就这个框架可以让我们像访问本地文件夹一下访问远程文件夹。

 

JCIFS的网址: http://jcifs.samba.org/

JCIFS是使用纯Java开发的一个开源框架,通过smb协议访问远程文件夹。该框架同时支持Windows共享文件夹和Linux共享文件夹,不过,Linux共享文件夹需要安装Samba服务软件(官网:http://www.samba.org/)。

SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。

本文主要学习一下使用Java访问Windows共享文件夹的方法。

首先找一台Windows机器,在任意位置创建文件夹:sharedFolder,并设置为共享,设置共享用户名:share,密码:admin。

(Windows7下设置共享文件夹方法:http://hi.baidu.com/51_xuexi/item/5a90a20cd732a8ce75cd3c6d

不论是Windows还是Linux的共享文件夹,使用Java smb访问共享文件夹的代码都是一样的,只是Windows与Linux配置共享文件夹的方式不一样。

测试代码如下:

 

  1. InputStream in = null;  
  2. OutputStream out = null;  
  3. try {  
  4.     //获取图片   
  5.     File localFile = new File("C:/test.jpg");  
  6.     String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/"; //存放图片的共享目录  
  7.     SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS_");  
  8.     SmbFile remoteFile = new SmbFile(remotePhotoUrl + "/" + fmt.format(new Date()) + localFile.getName());  
  9.     remoteFile.connect(); //尝试连接   
  10.   
  11.     in = new BufferedInputStream(new FileInputStream(localFile));  
  12.     out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));  
  13.   
  14.     byte[] buffer = new byte[4096];  
  15.     int len = 0; //读取长度   
  16.     while ((len = in.read(buffer, 0, buffer.length)) != -1) {  
  17.         out.write(buffer, 0, len);  
  18.     }  
  19.     out.flush(); //刷新缓冲的输出流   
  20. }  
  21. catch (Exception e) {  
  22.     String msg = "发生错误:" + e.getLocalizedMessage();  
  23.     System.out.println(msg);  
  24. }  
  25. finally {  
  26.     try {  
  27.         if(out != null) {  
  28.             out.close();  
  29.         }  
  30.         if(in != null) {  
  31.             in.close();  
  32.         }  
  33.     }  
  34.     catch (Exception e) {}  
  35. }  
        InputStream in = null;
        OutputStream out = null;
        try {
            //获取图片
            File localFile = new File("C:/test.jpg");
            String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/"; //存放图片的共享目录
            SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS_");
            SmbFile remoteFile = new SmbFile(remotePhotoUrl + "/" + fmt.format(new Date()) + 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) {}
        }

以上代码中,使用了JCIFS框架提供的SmbFile类,这个类和Java的File类比较相似,使用这个类的对象,可以处理远程文件的读写。使用File对象读取本地文件,然后使用SmbFile对象写入远程文件。SmbFile的connect()方法可以尝试连接远程文件夹,如果账号或密码错误,将抛出连接异常。

 

当下载远程文件时,使用SmbFile对象读取远程文件即可,代码如下:

 

  1. InputStream in = null ;  
  2. ByteArrayOutputStream out = null ;  
  3. try {  
  4.     //创建远程文件对象   
  5.     String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/test.jpg";  
  6.     SmbFile remoteFile = new SmbFile(remotePhotoUrl);  
  7.     remoteFile.connect(); //尝试连接   
  8.     //创建文件流   
  9.     in = new BufferedInputStream(new SmbFileInputStream(remoteFile));  
  10.     out = new ByteArrayOutputStream((int)remoteFile.length());  
  11.     //读取文件内容   
  12.     byte[] buffer = new byte[4096];  
  13.     int len = 0; //读取长度   
  14.     while ((len = in.read(buffer, 0, buffer.length)) != - 1) {  
  15.         out.write(buffer, 0, len);  
  16.     }  
  17.   
  18.     out.flush(); //刷新缓冲的输出流   
  19.     return out.toByteArray();  
  20. }  
  21. catch (Exception e) {  
  22.     String msg = "下载远程文件出错:" + e.getLocalizedMessage();  
  23.     System.out.println(msg);  
  24. }  
  25. finally {  
  26.     try {  
  27.         if(out != null) {  
  28.             out.close();  
  29.         }  
  30.         if(in != null) {  
  31.             in.close();  
  32.         }  
  33.     }  
  34.     catch (Exception e) {}  
  35. }  
        InputStream in = null ;
        ByteArrayOutputStream out = null ;
        try {
            //创建远程文件对象
            String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/test.jpg";
            SmbFile remoteFile = new SmbFile(remotePhotoUrl);
            remoteFile.connect(); //尝试连接
            //创建文件流
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new ByteArrayOutputStream((int)remoteFile.length());
            //读取文件内容
            byte[] buffer = new byte[4096];
            int len = 0; //读取长度
            while ((len = in.read(buffer, 0, buffer.length)) != - 1) {
                out.write(buffer, 0, len);
            }

            out.flush(); //刷新缓冲的输出流
            return out.toByteArray();
        }
        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) {}
        }

 

 

Java读写Windows共享文件夹 .

标签:

原文地址:http://www.cnblogs.com/firstdream/p/5293410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!