欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java利用SMB讀取遠(yuǎn)程文件的方法

 更新時(shí)間:2018年05月21日 15:18:42   作者:OkidoGreen  
這篇文章主要為大家詳細(xì)介紹了java利用SMB讀取遠(yuǎn)程文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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的中文亂碼問題

    這篇文章主要介紹了Java & Idea啟動(dòng)tomcat的中文亂碼問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot資源文件的存放位置設(shè)置方式

    SpringBoot資源文件的存放位置設(shè)置方式

    這篇文章主要介紹了SpringBoot資源文件的存放位置設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 詳解springmvc如何處理接受http請求

    詳解springmvc如何處理接受http請求

    這篇文章主要給大家介紹了springmvc如何處理接受http請求,文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • java無鎖hashmap原理與實(shí)現(xiàn)詳解

    java無鎖hashmap原理與實(shí)現(xiàn)詳解

    本文主要介紹了java無鎖hashmap原理與實(shí)現(xiàn),大家參考使用吧
    2014-01-01
  • 淺談Java分布式架構(gòu)下如何實(shí)現(xiàn)分布式鎖

    淺談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類的性能

    詳細(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-05
  • Spring 整合 MyBatis的實(shí)現(xiàn)步驟

    Spring 整合 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-02
  • Java中常見死鎖與活鎖的實(shí)例詳解

    Java中常見死鎖與活鎖的實(shí)例詳解

    這篇文章主要介紹了Java中常見死鎖與活鎖的實(shí)例詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • SpringBoot整合RabbitMQ實(shí)現(xiàn)交換機(jī)與隊(duì)列的綁定

    SpringBoot整合RabbitMQ實(shí)現(xiàn)交換機(jī)與隊(duì)列的綁定

    這篇文章將通過幾個(gè)實(shí)例為大家介紹一些SpringBoot中RabbitMQ如何綁定交換機(jī)(交換器)與隊(duì)列,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-05-05
  • Spring Boot 中的 @EnableDiscoveryClient 注解的原理

    Spring Boot 中的 @EnableDiscoveryClient 注解

    @EnableDiscoveryClient 注解是 Spring Boot 應(yīng)用程序注冊到服務(wù)注冊中心的關(guān)鍵注解,這篇文章主要介紹了Spring Boot 中的 @EnableDiscoveryClient 注解,需要的朋友可以參考下
    2023-07-07

最新評論