Java遠(yuǎn)程共享目錄的操作代碼
一.前言
根據(jù)客戶反饋,在進(jìn)行文件下載的時(shí)候,新增遠(yuǎn)程共享目錄,下載對(duì)應(yīng)的文件到遠(yuǎn)程共享目錄,采用常用的IO操作模式,提示下載成功,但是客戶去遠(yuǎn)程共享目錄查看對(duì)應(yīng)的下載文件,反饋說(shuō)沒(méi)有找到對(duì)應(yīng)的文件。要求系統(tǒng)需要支持上傳遠(yuǎn)程共享目錄,為什么有一個(gè)這樣的需求?由于下載的文件涉及到了支付文件,里面的金額不允許進(jìn)行修改,如果放在本地路徑有可能會(huì)不會(huì)出現(xiàn)人為的修改,一般涉及到錢的問(wèn)題,客戶都是比較謹(jǐn)慎的,剛好沒(méi)有接觸過(guò)操作遠(yuǎn)程共享目錄的,就google了一下看有沒(méi)有對(duì)應(yīng)的操作說(shuō)明,下面簡(jiǎn)單總結(jié)一下。
二.遠(yuǎn)程共享目錄操作
1、需要下載對(duì)應(yīng)的jcifs-1.3.18.jar,本例子采用3.18版本的,下載鏈接:https://jcifs.samba.org/
2、涉及的主要類是 SmbFile(遠(yuǎn)程文件操作類) ,還有就是進(jìn)行登錄驗(yàn)證,驗(yàn)證對(duì)應(yīng)的遠(yuǎn)程目錄的合法性的操作,其他操作就普通的IO流的操作。
3、從遠(yuǎn)程共享目錄下載文件
/** * 方法說(shuō)明:從遠(yuǎn)程共享目錄下載文件 * @param localDir 本地臨時(shí)路徑 * @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)程共享目錄
/** * 方法說(shuō)明:上傳文件到遠(yuǎn)程共享目錄 * @param localDir 本地臨時(shí)路徑(A:/測(cè)試/測(cè)試.xls) * @param removeDir 遠(yuǎn)程共享路徑(smb://10.169.2.xx/測(cè)試/,特殊路徑只能用/) * @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 可以在處理大文件時(shí),提升性能 * * @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)程共享目錄的操作代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Java使用wait() notify()方法操作共享資源詳解
- Java concurrency之共享鎖和ReentrantReadWriteLock_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- Java 存儲(chǔ)模型和共享對(duì)象詳解
- Java讀寫Windows共享文件夾的方法實(shí)例
- Java線程重復(fù)執(zhí)行以及操作共享變量的代碼示例
- Java中tomcat memecached session 共享同步問(wèn)題的解決辦法
- Java多線程編程之ThreadLocal線程范圍內(nèi)的共享變量
- Java編程多線程之共享數(shù)據(jù)代碼詳解
相關(guān)文章
Java調(diào)用shell命令涉及管道、重定向時(shí)不生效問(wèn)題及解決
這篇文章主要介紹了Java調(diào)用shell命令涉及管道、重定向時(shí)不生效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12基于Springboot疫苗接種行程管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)
本文主要介紹了基于Springboot實(shí)現(xiàn)的疫苗接種行程管理系統(tǒng)的示例代碼,系統(tǒng)主要實(shí)現(xiàn)個(gè)人疫苗接種管理、行程管理、病史管理、風(fēng)險(xiǎn)地區(qū)管理、核酸檢測(cè)報(bào)告結(jié)果上報(bào)、疫情新聞管理等功能,需要的可以參考一下2022-03-03在Java中如何避免創(chuàng)建不必要的對(duì)象
作為Java開發(fā)者,我們每天創(chuàng)建很多對(duì)象,但如何才能避免創(chuàng)建不必要的對(duì)象呢?這需要我們好好學(xué)習(xí),這篇文章主要給大家介紹了關(guān)于在Java中如何避免創(chuàng)建不必要對(duì)象的相關(guān)資料,需要的朋友可以參考下2021-10-10java實(shí)現(xiàn)時(shí)間與字符串之間轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)時(shí)間與字符串之間轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12

RestTemplate請(qǐng)求失敗自動(dòng)重啟機(jī)制精講

IDEA新建javaWeb以及Servlet簡(jiǎn)單實(shí)現(xiàn)小結(jié)

java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段

Spring?MVC中JSON數(shù)據(jù)處理方式實(shí)戰(zhàn)案例