java利用SMB讀取遠(yuǎn)程文件的方法
本文實(shí)例為大家分享了java利用SMB讀取遠(yuǎn)程文件的具體代碼,供大家參考,具體內(nèi)容如下
package com.yss.test.FileReadWriter; 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 java.net.MalformedURLException; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; import jcifs.smb.SmbFileOutputStream; public class RemoteAccessData { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { smbGet1("smb://192.168.75.204/test/新建 文本文檔.txt"); smbGet("smb://192.168.75.204/test/新建 文本文檔.txt","e:/"); } /** * 方法一: * * @param remoteUrl * 遠(yuǎn)程路徑 smb://192.168.75.204/test/新建 文本文檔.txt * @throws IOException */ public static void smbGet1(String remoteUrl) throws IOException { SmbFile smbFile = new SmbFile(remoteUrl); 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(); } // 從共享目錄下載文件 /** * 方法二: * 路徑格式:smb://192.168.75.204/test/新建 文本文檔.txt * smb://username:password@192.168.0.77/test * @param remoteUrl * 遠(yuǎn)程路徑 * @param localDir * 要寫入的本地路徑 */ public static 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]; } } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } // 向共享目錄上傳文件 public static void smbPut(String remoteUrl, String localFilePath) { InputStream in = null; OutputStream out = null; try { File localFile = new File(localFilePath); String fileName = localFile.getName(); SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName); in = new BufferedInputStream(new FileInputStream(localFile)); out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); byte[] buffer = new byte[1024]; while (in.read(buffer) != -1) { out.write(buffer); buffer = new byte[1024]; } } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } // 遠(yuǎn)程url smb://192.168.0.77/test // 如果需要用戶名密碼就這樣: // smb://username:password@192.168.0.77/test }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Java & Idea啟動(dòng)tomcat的中文亂碼問題
這篇文章主要介紹了Java & Idea啟動(dòng)tomcat的中文亂碼問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07java無鎖hashmap原理與實(shí)現(xiàn)詳解
本文主要介紹了java無鎖hashmap原理與實(shí)現(xiàn),大家參考使用吧2014-01-01淺談Java分布式架構(gòu)下如何實(shí)現(xiàn)分布式鎖
這篇文章主要介紹了淺談Java分布式架構(gòu)下如何實(shí)現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07詳細(xì)分析Java中String、StringBuffer、StringBuilder類的性能
在Java中,String類和StringBuffer類以及StringBuilder類都能用于創(chuàng)建字符串對象,而在分別操作這些對象時(shí)我們會(huì)發(fā)現(xiàn)JVM執(zhí)行它們的性能并不相同,下面我們就來詳細(xì)分析Java中String、StringBuffer、StringBuilder類的性能2016-05-05Spring 整合 MyBatis的實(shí)現(xiàn)步驟
SpringMVC 本來就是 Spring 框架的一部分,這兩者無須再做整合,所以 SSM 整合的關(guān)鍵就是Spring對MyBatis的整合,三大框架整合完成后,將以 Spring 為核心,調(diào)用有關(guān)資源,高效運(yùn)作,這篇文章主要介紹了 Spring 整合 MyBatis的實(shí)現(xiàn)步驟,需要的朋友可以參考下2023-02-02SpringBoot整合RabbitMQ實(shí)現(xiàn)交換機(jī)與隊(duì)列的綁定
這篇文章將通過幾個(gè)實(shí)例為大家介紹一些SpringBoot中RabbitMQ如何綁定交換機(jī)(交換器)與隊(duì)列,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05Spring Boot 中的 @EnableDiscoveryClient 注解
@EnableDiscoveryClient 注解是 Spring Boot 應(yīng)用程序注冊到服務(wù)注冊中心的關(guān)鍵注解,這篇文章主要介紹了Spring Boot 中的 @EnableDiscoveryClient 注解,需要的朋友可以參考下2023-07-07