午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java 通過(guò) SmbFile 類(lèi)操作共享文件夾的示例

瀏覽:13日期:2022-08-17 11:13:58
一、添加依賴(lài)

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

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

/** * 讀取共享文件夾下的所有文件(文件夾)的名稱(chēng) * @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(); }}三、創(chuàng)建文件夾

/** * 創(chuàng)建文件夾 * @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; // 讀取長(zhǎng)度 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); // 處理空格轉(zhuǎn)為加號(hào)的問(wèn)題 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(); }}

刪除文件夾將路徑指向要?jiǎng)h除的文件夾即可。

到此這篇關(guān)于java 通過(guò) SmbFile 類(lèi)操作共享文件夾的文章就介紹到這了,更多相關(guān)java操作共享文件夾內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产福利视频在线观看 | 国产超碰在线 | 国产激情网站 | 天天曰天天 | 久久99国产精品 | 亚洲国产影院 | www超碰 | 小视频在线播放 | 天堂中文在线视频 | 成人性生交大全免 | 男女羞羞网站 | 日韩在线视 | 国产黄色大片网站 | 日本aⅴ在线观看 | 在线免费中文字幕 | 国产精品麻豆传媒 | 91亚洲精品在线观看 | 超碰人人人人人人人 | 欧美日韩在线免费观看 | 亚洲综合一二三 | 久久久久久国产精品视频 | 亚洲综合视频网 | 91免费进入 | 一区二区三区精品在线观看 | 第一av | 超碰人人在线 | 久久99国产精品 | 欧美偷拍视频 | 另类天堂av | 激情午夜天 | 日本特黄一级片 | 在线免费观看亚洲 | 久操视频免费在线观看 | 亚洲午夜视频在线观看 | 欧美一级免费片 | 国产毛片欧美毛片久久久 | 欧美另类在线观看 | 亚洲国产精品久久久久久 | 国产夫妻精品 | 亚洲成人99 | 岛国精品在线播放 |