Java讀寫Windows共享文件夾 .


項目常常需要有訪問共享文件夾的需求,例如共享文件夾存儲照片、文件等。那么如何使用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) {}
        }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM