日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java访问远程共享文件

發(fā)布時間:2024/1/23 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java访问远程共享文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)載自:http://hu-bj.javaeye.com/blog/327198?
使用開源庫JCIFS?
http://jcifs.samba.org/?
  Microsoft使用NetBIOS實現(xiàn)了一個網(wǎng)絡(luò)文件/打印服?
務(wù)系統(tǒng),這個系統(tǒng)基于NetBIOS設(shè)定了一套文件共享協(xié)議,Microsoft稱之為SMB(?
Server Message Block)協(xié)議。這個協(xié)議被Microsoft用于它們Lan Manager和Wi?
ndows NT服務(wù)器系統(tǒng)中,而Windows系統(tǒng)均包括這個協(xié)議的客戶軟件,因而這個協(xié)?
議在局域網(wǎng)系統(tǒng)中影響很大。?
  隨著Internet的流行,Microsoft希望將這個協(xié)議擴展到Internet上去,成為?
Inter net上計算機之間相互共享數(shù)據(jù)的一種標準。因此它將原有的幾乎沒有多少?
技術(shù)文檔的SMB協(xié)議進行整理,重新命名為 CIFS(Common Internet File Syste?
m),并打算將它與NetBIOS相脫離,試圖使它成為Internet上的一個標準協(xié)議。?
  jcifs是CIFS在JAVA中的一個實現(xiàn),是samba組織負責維護開發(fā)的一個開源項目,?
專注于使用java語言對cifs協(xié)議的設(shè)計和實現(xiàn)。他們將jcifs設(shè)計成為一個完整的,豐?
富的,具有可擴展能力且線程安全的客戶端庫。這一庫可以應(yīng)用于各種java虛擬機訪?
問遵循CIFS/SMB網(wǎng)絡(luò)傳輸協(xié)議的網(wǎng)絡(luò)資源。?
示例:?
import java.io.BufferedInputStream;?
import java.io.BufferedOutputStream;?
import java.io.File;?
import java.io.FileOutputStream;?
import java.io.IOException;?
import java.io.InputStream;?
import java.io.OutputStream;?
import java.util.Date;?
import jcifs.smb.SmbFile;?
import jcifs.smb.SmbFileInputStream;?
public class RemoteShareFile {?
????? public static void main(String[] args) {?
????????? String smbMachine="smb://用戶名:密碼@IP地址/目錄/文件名";?
????????? String localPath="D://";?
????????? File file=readFromSmb(smbMachine,localPath);?
????? }?
????? public static File readFromSmb(String smbMachine,String localpath){?
????????? File localfile=null;?
????????? InputStream bis=null;?
????????? OutputStream bos=null;?
????????? try {?
????????????? SmbFile rmifile = new SmbFile(smbMachine);?
????????????? String filename=rmifile.getName();?
????????????? bis=new BufferedInputStream(new SmbFileInputStream(rmifile));?
????????????? localfile=new File(localpath+File.separator+filename);?
????????????? bos=new BufferedOutputStream(new FileOutputStream(localfile));?
????????????? int length=rmifile.getContentLength();?
????????????? byte[] buffer=new byte[length];?
????????????? Date date=new Date();?
????????????? bis.read(buffer);?
????????????? bos.write(buffer);?
????????????? Date end=new Date();?
????????????? int time= (int) ((end.getTime()-date.getTime())/1000);?
????????????? if(time>0)?
????????????????? System.out.println("用時:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");??????????
????????? } catch (Exception e){?
????????????? System.out.println(e.getMessage());?
????????? }finally{?
????????????? try {?
????????????????? bos.close();?
????????????????? bis.close();?
????????????? } catch (IOException e) {?
????????????????? e.printStackTrace();?
????????????? }?
????????? }?
????????? return localfile;?
???? }?

}

jcifs 項目地址 http://jcifs.samba.org/?

import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream;public class ReadShareFile {public static void main(String[] args) {try {SmbFile smbFile = new SmbFile("smb://test:test@192.168.1.1/out/test.txt");int length = smbFile.getContentLength();// 得到文件的大小byte buffer[] = new byte[length];SmbFileInputStream in = new SmbFileInputStream(smbFile);// 建立smb文件輸入流while ((in.read(buffer)) != -1) {System.out.write(buffer);System.out.println(buffer.length);}in.close();} catch (Exception e) {e.printStackTrace();}} }
例2:

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Date;import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream;public class TestReadSmb { public static void main(String[] args){ String smbMachine="smb://test:test@10.108.23.200/temp/test.txt";String localPath="D:\\temp"; File file=readFromSmb(smbMachine,localPath); removeFile(file); } /** *** * 從smbMachine讀取文件并存儲到localpath指定的路徑 * * @param smbMachine * 共享機器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/測試文本.txt,xxx:xxx是共享機器的用戶名密碼 * @param localpath * 本地路徑 * @return */ public static File readFromSmb(String smbMachine,String localpath){ File localfile=null; InputStream bis=null; OutputStream bos=null; try{ SmbFile rmifile = new SmbFile(smbMachine); String filename=rmifile.getName(); bis=new BufferedInputStream(new SmbFileInputStream(rmifile)); localfile=new File(localpath+File.separator+filename); System.out.println("localfile=="+localfile);bos=new BufferedOutputStream(new FileOutputStream(localfile)); int length=rmifile.getContentLength(); System.out.println("length=="+length);byte[] buffer=new byte[length]; Date date=new Date(); bis.read(buffer); bos.write(buffer); Date end=new Date(); int time= (int) ((end.getTime()-date.getTime())/1000); if(time>0) System.out.println("用時:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒"); } catch (Exception e){ System.out.println(e.getMessage()); }finally{ try { bos.close(); bis.close(); } catch (IOException e) { e.printStackTrace(); } } return localfile; } public static boolean removeFile(File file) { return file.delete(); } }

總結(jié)

以上是生活随笔為你收集整理的java访问远程共享文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。