版權聲明:本文為博主原創文章,未經博主允許不得轉載。
項目常常需要有訪問共享文件夾的需求,例如共享文件夾存儲照片、文件等。那么如何使用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配置共享文件夾的方式不一樣。
測試代碼如下:
- 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) {}
- }
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對象讀取遠程文件即可,代碼如下:
- 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) {}
- }
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) {} }