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
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決IntelliJ IDEA創(chuàng)建spring boot無法連接http://start.spring.io/問題
這篇文章主要介紹了解決IntelliJ IDEA創(chuàng)建spring boot無法連接http://start.spring.io/問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
SpringBoot整合SpringTask實(shí)現(xiàn)定時(shí)任務(wù)的流程
這篇文章主要介紹了SpringBoot整合SpringTask實(shí)現(xiàn)定時(shí)任務(wù)的流程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
java 中newInstance()方法和new關(guān)鍵字的區(qū)別
這篇文章主要介紹了java 中newInstance()方法和new關(guān)鍵字的區(qū)別的相關(guān)資料,希望通過本文大家能掌握他們之家的區(qū)別與用法,需要的朋友可以參考下2017-09-09
springboot 如何取消starter的自動(dòng)注入
這篇文章主要介紹了springboot 如何取消starter的自動(dòng)注入操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java并發(fā)工具之CyclicBarrier使用詳解
這篇文章主要介紹了Java并發(fā)工具之CyclicBarrier使用詳解,CyclicBarrier是一個(gè)同步器,允許一組線程相互之間等待,直到到達(dá)某個(gè)公共屏障點(diǎn)(common barrier point),再繼續(xù)執(zhí)行,需要的朋友可以參考下2023-12-12
使用Spring的ApplicationEvent實(shí)現(xiàn)本地事件驅(qū)動(dòng)的實(shí)現(xiàn)方法
本文介紹了如何使用Spring的ApplicationEvent實(shí)現(xiàn)本地事件驅(qū)動(dòng),通過自定義事件和監(jiān)聽器,實(shí)現(xiàn)模塊之間的松耦合,提升代碼的可維護(hù)性和擴(kuò)展性。同時(shí)還介紹了異步事件和事件傳遞的相關(guān)知識(shí)2023-04-04
JDBC實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了JDBC實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02

