SpringBoot訪問windows共享文件的方法
前言
最近有項目需要開發(fā)檔案打包下載功能,其中包含很多大附件,項目使用minio存儲且不在同一臺服務器上,為了優(yōu)化速度決定使用windows共享功能進行文件傳輸
SMB1.0
集成jcifs類庫,主要適用于一些老舊系統(tǒng),但下載速度比較慢,僅作參考
此類庫沒有maven引用,官網地址:http://jcifs.samba.org/
注意事項:
設置jcifs.smb.client.dfs.disabled選項開啟,可以提高傳輸速度
使用NtlmPasswordAuthentication認證代替smb協(xié)議url攜帶用戶名密碼方式,避免特殊字符傳遞造成認證失敗
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),默認安裝開啟無需額外配置
此類庫maven引用很久沒有發(fā)布最新版本,需要下載代碼自行編譯,github地址:https://github.com/hierynomus/smbj
經測試,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è)/政府項目為了系統(tǒng)安全會禁用445端口,而445端口禁用后文件共享功能無法使用,此時我們需要進行端口轉發(fā),即將客戶端445端口轉發(fā)到共享服務器端口A,共享服務器將本地端口A轉發(fā)到445即可完成共享,具體操作步驟如下,192.168.1.164就是共享文件服務器的內網ip
查看服務器轉發(fā)規(guī)則
netsh interface portproxy show all
刪除服務器轉發(fā)規(guī)則
netsh interface portproxy reset
共享文件服務器
- 執(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
客戶端服務器
- 關閉Server服務
- CMD執(zhí)行代碼
netsh interface portproxy add v4tov4 listenaddress=127.0.0.1 listenport=445 connectaddress=192.168.1.164 connectport=4455
- 重啟系統(tǒng)
到此這篇關于SpringBoot訪問windows共享文件的文章就介紹到這了,更多相關SpringBoot訪問windows共享文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java實現(xiàn)簡易超市管理系統(tǒng) 附源碼下載
這篇文章主要介紹了java實現(xiàn)簡易超市管理系統(tǒng)(含源碼),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Java中stream處理中map與flatMap的比較和使用案例
這篇文章主要介紹了Java中stream處理中map與flatMap的比較和使用案例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03解決SpringBoot運行Test時報錯:SpringBoot Unable to find
這篇文章主要介紹了SpringBoot運行Test時報錯:SpringBoot Unable to find a @SpringBootConfiguration,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10