java 通过 SmbFile 类操作共享文件夹的示例

 更新时间:2021年02月04日 09:45:25   作者:素小暖  
这篇文章主要介绍了java 通过 SmbFile 类操作共享文件夹,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、添加依赖

在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操作共享文件夹内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Mybatis 大数据量批量写优化的案例详解

    Mybatis 大数据量批量写优化的案例详解

    这篇文章主要介绍了Mybatis 大数据量批量写优化的示例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • nohup运行Java tail查看日志方式

    nohup运行Java tail查看日志方式

    nohup命令允许程序在用户退出账户或关闭终端后继续运行,常与"&"结合使用以实现程序的后台执行,配合重定向操作,nohup可以将程序输出保存到日志文件中,如nohup java -jar XXX.jar &> myout.log &,此外,tail命令可用于实时监控日志文件的变化
    2024-09-09
  • Jmeter逻辑控制器事务控制器使用方法解析

    Jmeter逻辑控制器事务控制器使用方法解析

    这篇文章主要介绍了Jmeter逻辑控制器事务控制器使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • 详解使用Spring Security OAuth 实现OAuth 2.0 授权

    详解使用Spring Security OAuth 实现OAuth 2.0 授权

    本篇文章主要介绍了详解使用Spring Security OAuth 实现OAuth 2.0 授权,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • 详解MybatisPlus集成nacos导致druid连接不上数据库

    详解MybatisPlus集成nacos导致druid连接不上数据库

    这篇文章主要介绍了详解MybatisPlus集成nacos导致druid连接不上数据库,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Spring Boot中使用Spring-Retry重试框架的实现

    Spring Boot中使用Spring-Retry重试框架的实现

    本文主要介绍了Spring Boot中使用Spring-Retry重试框架的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • Java Calendar日历类的使用介绍

    Java Calendar日历类的使用介绍

    Candendar类是一个抽象类,提供了一些获取当前时间,或者指定的时间的字段和一些方法,我们可以通过一些方法与字段对他进行获取当前天或者当月的一些信息
    2022-09-09
  • SpringMVC后端返回数据到前端代码示例

    SpringMVC后端返回数据到前端代码示例

    这篇文章主要介绍了SpringMVC后端返回数据到前端代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Java日常练习题,每天进步一点点(13)

    Java日常练习题,每天进步一点点(13)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以帮到你
    2021-07-07
  • SpringBoot+Spring Security基于内存用户认证的实现

    SpringBoot+Spring Security基于内存用户认证的实现

    本文介绍了SpringBoot+Spring Security基于内存用户认证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-11-11

最新评论