Java遠(yuǎn)程共享目錄的操作代碼
一.前言
根據(jù)客戶反饋,在進(jìn)行文件下載的時候,新增遠(yuǎn)程共享目錄,下載對應(yīng)的文件到遠(yuǎn)程共享目錄,采用常用的IO操作模式,提示下載成功,但是客戶去遠(yuǎn)程共享目錄查看對應(yīng)的下載文件,反饋說沒有找到對應(yīng)的文件。要求系統(tǒng)需要支持上傳遠(yuǎn)程共享目錄,為什么有一個這樣的需求?由于下載的文件涉及到了支付文件,里面的金額不允許進(jìn)行修改,如果放在本地路徑有可能會不會出現(xiàn)人為的修改,一般涉及到錢的問題,客戶都是比較謹(jǐn)慎的,剛好沒有接觸過操作遠(yuǎn)程共享目錄的,就google了一下看有沒有對應(yīng)的操作說明,下面簡單總結(jié)一下。
二.遠(yuǎn)程共享目錄操作
1、需要下載對應(yīng)的jcifs-1.3.18.jar,本例子采用3.18版本的,下載鏈接:https://jcifs.samba.org/
2、涉及的主要類是 SmbFile(遠(yuǎn)程文件操作類) ,還有就是進(jìn)行登錄驗(yàn)證,驗(yàn)證對應(yīng)的遠(yuǎn)程目錄的合法性的操作,其他操作就普通的IO流的操作。
3、從遠(yuǎn)程共享目錄下載文件
/**
* 方法說明:從遠(yuǎn)程共享目錄下載文件
* @param localDir 本地臨時路徑
* @param removeDir 遠(yuǎn)程共享路徑
* @param _fileName 遠(yuǎn)程共享文件名
* @param removeIp 遠(yuǎn)程共享目錄IP
* @param removeLoginUser 遠(yuǎn)程共享目錄用戶名
* @param removeLoginPass 遠(yuǎn)程共享目錄密碼
* @return
* @throws Exception
*/
public static int smbDownload(String localDir, String removeDir,
String _fileName, String removeIp, String removeLoginUser,
String removeLoginPass) throws Exception {
InputStream in = null;
OutputStream out = null;
try {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
removeIp, removeLoginUser, removeLoginPass);
SmbFile remoteFile = new SmbFile(removeDir + _fileName, auth);
if (!remoteFile.exists()) {
return 0;
}
File dir = new File(localDir);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = _fileName.substring(_fileName.lastIndexOf("\\")+1, _fileName.length());
File localFile = new File(localDir + 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 {
if (null != out) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return 1;
}
4、上傳文件都遠(yuǎn)程共享目錄
/**
* 方法說明:上傳文件到遠(yuǎn)程共享目錄
* @param localDir 本地臨時路徑(A:/測試/測試.xls)
* @param removeDir 遠(yuǎn)程共享路徑(smb://10.169.2.xx/測試/,特殊路徑只能用/)
* @param removeIp 遠(yuǎn)程共享目錄IP(10.169.2.xx)
* @param removeLoginUser 遠(yuǎn)程共享目錄用戶名(user)
* @param removeLoginPass 遠(yuǎn)程共享目錄密碼(password)
* @return
* @throws Exception 0成功/-1失敗
*/
public static int smbUploading(String localDir, String removeDir,
String removeIp, String removeLoginUser, String removeLoginPass) throws Exception {
NtlmPasswordAuthentication auth = null;
OutputStream out = null;
int retVal = 0;
try {
File dir = new File(localDir);
if (!dir.exists()) {
dir.mkdirs();
}
InetAddress ip = InetAddress.getByName(removeIp);
UniAddress address = new UniAddress(ip);
// 權(quán)限驗(yàn)證
auth = new NtlmPasswordAuthentication(removeIp, removeLoginUser, removeLoginPass);
SmbSession.logon(address,auth);
//遠(yuǎn)程路徑判斷文件文件路徑是否合法
SmbFile remoteFile = new SmbFile(removeDir + dir.getName(), auth);
remoteFile.connect();
if(remoteFile.isDirectory()){
retVal = -1;
}
// 向遠(yuǎn)程共享目錄寫入文件
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
out.write(toByteArray(dir));
} catch (UnknownHostException e) {
retVal = -1;
e.printStackTrace();
} catch (MalformedURLException e) {
retVal = -1;
e.printStackTrace();
} catch (SmbException e) {
retVal = -1;
e.printStackTrace();
} catch (IOException e) {
retVal = -1;
e.printStackTrace();
} finally{
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return retVal;
}
/**
* Mapped File way MappedByteBuffer 可以在處理大文件時,提升性能
*
* @param file 文件
* @return 字節(jié)數(shù)組
* @throws IOException IO異常信息
*/
@SuppressWarnings("resource")
public static byte[] toByteArray(File file) throws IOException {
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,
fc.size()).load();
byte[] result = new byte[(int) fc.size()];
if (byteBuffer.remaining() > 0) {
byteBuffer.get(result, 0, byteBuffer.remaining());
}
return result;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
總結(jié)
以上所述是小編給大家介紹的Java遠(yuǎn)程共享目錄的操作代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
關(guān)于springcloud報錯報UnsatisfiedDependencyException的問題
這篇文章主要介紹了關(guān)于springcloud報錯報UnsatisfiedDependencyException的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
springBoot項(xiàng)目啟動類啟動無法訪問的解決方法
這篇文章主要介紹了springBoot項(xiàng)目啟動類啟動無法訪問的解決方法,需要的朋友可以參考下2018-10-10
springboot使用定時器@Scheduled不管用的解決
這篇文章主要介紹了springboot使用定時器@Scheduled不管用的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
MyBatis中通用SQL寫法的幾種方法實(shí)現(xiàn)
本文介紹了MyBatis中常見的幾種通用SQL寫法,包括批量操作、動態(tài)SQL、多條件分支查詢、SQL語句優(yōu)化、自動生成主鍵、注解方式以及高級映射等,感興趣的可以了解一下2024-12-12
Spring Boot 配置隨機(jī)數(shù)的技巧代碼詳解
這篇文章主要介紹了Spring Boot 配置隨機(jī)數(shù)技巧,spring boot 支持在系統(tǒng)加載的時候配置隨機(jī)數(shù),具體實(shí)例代碼大家參考下本文2018-05-05
SpringMVC中的SimpleUrlHandlerMapping用法詳解
這篇文章主要介紹了SpringMVC中的SimpleUrlHandlerMapping用法詳解,SimpleUrlHandlerMapping是Spring MVC中適用性最強(qiáng)的Handler Mapping類,允許明確指定URL模式和Handler的映射關(guān)系,有兩種方式聲明SimpleUrlHandlerMapping,需要的朋友可以參考下2023-10-10

