SpringBoot訪問windows共享文件的方法
前言
最近有項(xiàng)目需要開發(fā)檔案打包下載功能,其中包含很多大附件,項(xiàng)目使用minio存儲且不在同一臺服務(wù)器上,為了優(yōu)化速度決定使用windows共享功能進(jìn)行文件傳輸
SMB1.0
集成jcifs類庫,主要適用于一些老舊系統(tǒng),但下載速度比較慢,僅作參考
此類庫沒有maven引用,官網(wǎng)地址:http://jcifs.samba.org/
注意事項(xiàng):
設(shè)置jcifs.smb.client.dfs.disabled選項(xiàng)開啟,可以提高傳輸速度
使用NtlmPasswordAuthentication認(rèn)證代替smb協(xié)議url攜帶用戶名密碼方式,避免特殊字符傳遞造成認(rèn)證失敗
public static void downloadFile(String ip, String shareFolder, String filePath, String localDir) throws Exception { System.setProperty("jcifs.smb.client.dfs.disabled", "true"); String url = getFileUrl(ip, shareFolder, filePath); SmbFile smbFile = new SmbFile(url); smbFile.connect(); FileUtil.initfloderPath(localDir); String localFilePath = localDir + "/" + smbFile.getName(); BufferedInputStream buf = new BufferedInputStream(new SmbFileInputStream(smbFile)); FileUtil.writeFile(localFilePath, FileUtil.convertStreamToByte(buf)); } public static void downloadFileByAuth(String ip, String shareFolder, String userName, String password, String filePath, String localDir) throws Exception { System.setProperty("jcifs.smb.client.dfs.disabled", "true"); String url = getFileUrl(ip, shareFolder, filePath); NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, userName, password); SmbFile smbFile = new SmbFile(url, auth); smbFile.connect(); FileUtil.initfloderPath(localDir); String localFilePath = localDir + "/" + smbFile.getName(); BufferedInputStream buf = new BufferedInputStream(new SmbFileInputStream(smbFile)); FileUtil.writeFile(localFilePath, FileUtil.convertStreamToByte(buf)); } public static String getFileUrl(String ip, String shareFolder, String filePath) { return "smb://" + ip + "/" + shareFolder + "/" + filePath; }
SMB2.0
集成smbj類庫,適用于windows server2012及以上操作系統(tǒng),默認(rèn)安裝開啟無需額外配置
此類庫maven引用很久沒有發(fā)布最新版本,需要下載代碼自行編譯,github地址:https://github.com/hierynomus/smbj
經(jīng)測試,500MB文件傳輸大概比minio協(xié)議傳輸快了4秒左右,小文件傳輸速度基本保持一致
public static void downloadFileV2(String ip, String shareFolder, String filePath, String localDir) throws Exception { SMBClient client = new SMBClient(SmbConfig.createDefaultConfig()); Connection conn = client.connect(ip); Session session = conn.authenticate(AuthenticationContext.anonymous()); downLoadSMB2(session, shareFolder, filePath, localDir); } public static void downloadFileByAuthV2(String ip, String shareFolder, String userName, String password, String filePath, String localDir) throws Exception { SMBClient client = new SMBClient(SmbConfig.createDefaultConfig()); Connection conn = client.connect(ip); Session session = conn.authenticate(new AuthenticationContext(userName, password.toCharArray(), ip)); downLoadSMB2(session, shareFolder, filePath, localDir); } private static void downLoadSMB2(Session session, String shareFolder, String filePath, String localDir) throws Exception { InputStream fis = null; FileOutputStream os = null; DiskShare diskShare = null; try { diskShare = (DiskShare) session.connectShare(shareFolder); if (!diskShare.fileExists(filePath)) { throw new FileNotFoundException(filePath); } if (!diskShare.isConnected()) diskShare = (DiskShare) session.connectShare(shareFolder); com.hierynomus.smbj.share.File file = diskShare.openFile(filePath, EnumSet.of(AccessMask.GENERIC_READ), (Set) null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, (Set) null ); fis = file.getInputStream(); FileUtil.initfloderPath(localDir); String[] filePathList = filePath.split("\\/"); String localFilePath = localDir + "/" + filePathList[filePathList.length - 1]; os = new FileOutputStream(localFilePath); byte[] b = new byte[4096]; int length; while ((length = fis.read(b)) > 0) { os.write(b, 0, length); } } catch (IOException e) { throw e; } finally { IOUtils.close(os); IOUtils.close(fis); if (diskShare != null && diskShare.isConnected()) diskShare.close(); } }
445端口被禁用解決辦法
一般企業(yè)/政府項(xiàng)目為了系統(tǒng)安全會(huì)禁用445端口,而445端口禁用后文件共享功能無法使用,此時(shí)我們需要進(jìn)行端口轉(zhuǎn)發(fā),即將客戶端445端口轉(zhuǎn)發(fā)到共享服務(wù)器端口A,共享服務(wù)器將本地端口A轉(zhuǎn)發(fā)到445即可完成共享,具體操作步驟如下,192.168.1.164就是共享文件服務(wù)器的內(nèi)網(wǎng)ip
查看服務(wù)器轉(zhuǎn)發(fā)規(guī)則
netsh interface portproxy show all
刪除服務(wù)器轉(zhuǎn)發(fā)規(guī)則
netsh interface portproxy reset
共享文件服務(wù)器
- 執(zhí)行CMD代碼
netsh interface portproxy add v4tov4 listenport=4455 listenaddress=192.168.1.164 connectport=445 connectaddress=127.0.0.1 netsh interface portproxy add v4tov4 listenport=4455 listenaddress=127.0.0.1 connectport=445 connectaddress=127.0.0.1
客戶端服務(wù)器
- 關(guān)閉Server服務(wù)
- CMD執(zhí)行代碼
netsh interface portproxy add v4tov4 listenaddress=127.0.0.1 listenport=445 connectaddress=192.168.1.164 connectport=4455
- 重啟系統(tǒng)
到此這篇關(guān)于SpringBoot訪問windows共享文件的文章就介紹到這了,更多相關(guān)SpringBoot訪問windows共享文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)簡單超市管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單超市管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01java實(shí)現(xiàn)簡易超市管理系統(tǒng) 附源碼下載
這篇文章主要介紹了java實(shí)現(xiàn)簡易超市管理系統(tǒng)(含源碼),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03詳解Java實(shí)現(xiàn)設(shè)計(jì)模式之責(zé)任鏈模式
責(zé)任鏈模式是一種行為設(shè)計(jì)模式,允許你將請求沿著處理鏈發(fā)送,然后處理者都可對其進(jìn)行處理,完成后可以再將其傳遞給下一個(gè)處理者。下面將會(huì)舉例說明什么是責(zé)任鏈模式,責(zé)任鏈模式該如何使用2021-06-06Java中stream處理中map與flatMap的比較和使用案例
這篇文章主要介紹了Java中stream處理中map與flatMap的比較和使用案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java RabbitMQ 中的消息長期不消費(fèi)會(huì)過期嗎
RabbitMQ支持消息的過期時(shí)間,在消息發(fā)送時(shí)可以進(jìn)行指定。 RabbitMQ支持隊(duì)列的過期時(shí)間,從消息入隊(duì)列開始計(jì)算,只要超過了隊(duì)列的超時(shí)時(shí)間配置,那么消息會(huì)自動(dòng)的清除2021-09-09解決SpringBoot運(yùn)行Test時(shí)報(bào)錯(cuò):SpringBoot Unable to find
這篇文章主要介紹了SpringBoot運(yùn)行Test時(shí)報(bào)錯(cuò):SpringBoot Unable to find a @SpringBootConfiguration,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10gateway和jwt網(wǎng)關(guān)認(rèn)證實(shí)現(xiàn)過程解析
這篇文章主要介紹了gateway和jwt網(wǎng)關(guān)認(rèn)證實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11