亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

java 通過 SmbFile 類操作共享文件夾的示例

瀏覽:9日期:2022-08-17 11:13:58
一、添加依賴

在pom.xml文件夾中添加如下的依賴就可以引用SmbFile類的jar包。

<dependency> <groupId>jcifs</groupId> <artifactId>jcifs</artifactId> <version>1.3.17</version></dependency>二、讀取文件

/** * 讀取共享文件夾下的所有文件(文件夾)的名稱 * @param remoteUrl */public static void getSharedFileList(String remoteUrl) { SmbFile smbFile; try { // smb://userName:passWord@host/path/ smbFile = new SmbFile(remoteUrl); if (!smbFile.exists()) { System.out.println('no such folder'); } else { SmbFile[] files = smbFile.listFiles(); for (SmbFile f : files) { System.out.println(f.getName()); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (SmbException e) { e.printStackTrace(); }}三、創建文件夾

/** * 創建文件夾 * @param remoteUrl * @param folderName * @return */public static void smbMkDir(String remoteUrl, String folderName) { SmbFile smbFile; try { // smb://userName:passWord@host/path/folderName smbFile = new SmbFile(remoteUrl + folderName); if (!smbFile.exists()) { smbFile.mkdir(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (SmbException e) { e.printStackTrace(); }}四、上傳文件

/** * 上傳文件 * @param remoteUrl * @param shareFolderPath * @param localFilePath * @param fileName */public static void uploadFileToSharedFolder(String remoteUrl, String shareFolderPath, String localFilePath, String fileName) { InputStream inputStream = null; OutputStream outputStream = null; try { File localFile = new File(localFilePath); inputStream = new FileInputStream(localFile); // smb://userName:passWord@host/path/shareFolderPath/fileName SmbFile smbFile = new SmbFile(remoteUrl + shareFolderPath + '/' + fileName); smbFile.connect(); outputStream = new SmbFileOutputStream(smbFile); byte[] buffer = new byte[4096]; int len = 0; // 讀取長度 while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) { outputStream.write(buffer, 0, len); } // 刷新緩沖的輸出流 outputStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { outputStream.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } }}五、下載文件

/** * 下載文件到瀏覽器 * @param httpServletResponse * @param remoteUrl * @param shareFolderPath * @param fileName */public static void downloadFileToBrowser(HttpServletResponse httpServletResponse, String remoteUrl, String shareFolderPath, String fileName) { SmbFile smbFile; SmbFileInputStream smbFileInputStream = null; OutputStream outputStream = null; try { // smb://userName:passWord@host/path/shareFolderPath/fileName smbFile = new SmbFile(remoteUrl + shareFolderPath + '/' + fileName); smbFileInputStream = new SmbFileInputStream(smbFile); httpServletResponse.setHeader('content-type', 'application/octet-stream'); httpServletResponse.setContentType('application/vnd.ms-excel;charset=UTF-8'); httpServletResponse.setHeader('Content-disposition', 'attachment; filename=' + fileName); // 處理空格轉為加號的問題 httpServletResponse.setHeader('Content-Disposition', 'attachment; fileName=' + fileName + ';filename*=utf-8’’' + URLEncoder.encode(fileName, 'UTF-8').replaceAll('+', '%20')); outputStream = httpServletResponse.getOutputStream(); byte[] buff = new byte[2048]; int len; while ((len = smbFileInputStream.read(buff)) != -1) { outputStream.write(buff, 0, len); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (SmbException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { outputStream.close(); smbFileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }}

/** * 下載文件到指定文件夾 * @param remoteUrl * @param shareFolderPath * @param fileName * @param localDir */public static void downloadFileToFolder(String remoteUrl, String shareFolderPath, String fileName, String localDir) { InputStream in = null; OutputStream out = null; try { SmbFile remoteFile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName); 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(); } }}六、刪除文件

/** * 刪除文件 * @param remoteUrl * @param shareFolderPath * @param fileName */public static void deleteFile(String remoteUrl, String shareFolderPath, String fileName) { SmbFile SmbFile; try { // smb://userName:passWord@host/path/shareFolderPath/fileName SmbFile = new SmbFile(remoteUrl + shareFolderPath + '/' + fileName); if (SmbFile.exists()) { SmbFile.delete(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (SmbException e) { e.printStackTrace(); }}

刪除文件夾將路徑指向要刪除的文件夾即可。

到此這篇關于java 通過 SmbFile 類操作共享文件夾的文章就介紹到這了,更多相關java操作共享文件夾內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 韩国黄色一级视频 | 精品一区精品二区 | 亚洲一区不卡 | 国产理论| 中国三级毛片 | 在线观看黄a | 国产福利视精品永久免费 | 国产成人精品日本亚洲语音1 | 一区二区三区网站在线免费线观看 | 福利社区在线观看 | 国产1区2区3区在线观看 | 久久99国产精品久久 | 亚洲色图88| 丝袜美腿亚洲综合 | 久久国产精品一区二区 | 国产亚洲欧美在线观看的 | 57pao国产成永久免费视频 | 国产成人综合网在线观看 | 国产成人久视频免费 | 最新欧美精品一区二区三区 | 亚洲精品美女久久久 | 国产乱码精品一区二区三区四川人 | 黄色小视频观看 | 精品国产中文一级毛片在线看 | 999香蕉视频 | 欧美性猛交xxxx乱大交蜜桃 | 一级毛片aaaaaa免费看 | 亚洲视频在线一区二区 | 色婷婷香蕉 | 青青青青手机在线视频观看国产 | 免费无遮挡嘿嘿嘿视频动态 | 伊人天天| 久久成人精品视频 | 黄色一级视频网站 | 亚洲无av码一区二区三区 | 欧美视频一区在线 | 一区二区三区免费视频 www | 1024手机最新手机在线 | 亚洲综合精品香蕉久久网97 | 国产精品不卡 | 在线观看亚洲欧美 |