最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年02月04日 09:45:25   作者:素小暖  
這篇文章主要介紹了java 通過 SmbFile 類操作共享文件夾,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、添加依賴

在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();
 }
}

三、創(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)的問題
  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 通過 SmbFile 類操作共享文件夾的文章就介紹到這了,更多相關(guān)java操作共享文件夾內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis?大數(shù)據(jù)量批量寫優(yōu)化的案例詳解

    Mybatis?大數(shù)據(jù)量批量寫優(yōu)化的案例詳解

    這篇文章主要介紹了Mybatis?大數(shù)據(jù)量批量寫優(yōu)化的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • nohup運(yùn)行Java tail查看日志方式

    nohup運(yùn)行Java tail查看日志方式

    nohup命令允許程序在用戶退出賬戶或關(guān)閉終端后繼續(xù)運(yùn)行,常與"&"結(jié)合使用以實(shí)現(xiàn)程序的后臺(tái)執(zhí)行,配合重定向操作,nohup可以將程序輸出保存到日志文件中,如nohup java -jar XXX.jar &> myout.log &,此外,tail命令可用于實(shí)時(shí)監(jiān)控日志文件的變化
    2024-09-09
  • Jmeter邏輯控制器事務(wù)控制器使用方法解析

    Jmeter邏輯控制器事務(wù)控制器使用方法解析

    這篇文章主要介紹了Jmeter邏輯控制器事務(wù)控制器使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 詳解使用Spring Security OAuth 實(shí)現(xiàn)OAuth 2.0 授權(quán)

    詳解使用Spring Security OAuth 實(shí)現(xiàn)OAuth 2.0 授權(quán)

    本篇文章主要介紹了詳解使用Spring Security OAuth 實(shí)現(xiàn)OAuth 2.0 授權(quán),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • 詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫(kù)

    詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫(kù)

    這篇文章主要介紹了詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫(kù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Spring Boot中使用Spring-Retry重試框架的實(shí)現(xiàn)

    Spring Boot中使用Spring-Retry重試框架的實(shí)現(xiàn)

    本文主要介紹了Spring Boot中使用Spring-Retry重試框架的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Java Calendar日歷類的使用介紹

    Java Calendar日歷類的使用介紹

    Candendar類是一個(gè)抽象類,提供了一些獲取當(dāng)前時(shí)間,或者指定的時(shí)間的字段和一些方法,我們可以通過一些方法與字段對(duì)他進(jìn)行獲取當(dāng)前天或者當(dāng)月的一些信息
    2022-09-09
  • SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例

    SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例

    這篇文章主要介紹了SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(13)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(13)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • SpringBoot+Spring Security基于內(nèi)存用戶認(rèn)證的實(shí)現(xiàn)

    SpringBoot+Spring Security基于內(nèi)存用戶認(rèn)證的實(shí)現(xiàn)

    本文介紹了SpringBoot+Spring Security基于內(nèi)存用戶認(rèn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11

最新評(píng)論

苏州市| 永仁县| 河津市| 平昌县| 大洼县| 长春市| 嵊泗县| 休宁县| 抚顺县| 扬州市| 岳阳市| 淮阳县| 六枝特区| 阿尔山市| 本溪市| 手机| 龙陵县| 鞍山市| 新闻| 仁怀市| 灵川县| 宜丰县| 星子县| 隆尧县| 南江县| 凌海市| 泸水县| 尉氏县| 岱山县| 旬邑县| 明水县| 金川县| 保定市| 通许县| 旬阳县| 丹东市| 四川省| 荃湾区| 望江县| 鹤庆县| 东城区|