服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java 通过 SmbFile 类操作共享文件夹的示例

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

2021-08-03 11:25素小暖 Java教程

这篇文章主要介绍了java 通过 SmbFile 类操作共享文件夹,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、添加依赖

在pom.xml文件夹中添加如下的依赖就可以引用SmbFile类的jar包。

  1. <dependency>
  2. <groupId>jcifs</groupId>
  3. <artifactId>jcifs</artifactId>
  4. <version>1.3.17</version>
  5. </dependency>

二、读取文件

  1. /**
  2. * 读取共享文件夹下的所有文件(文件夹)的名称
  3. * @param remoteUrl
  4. */
  5. public static void getSharedFileList(String remoteUrl) {
  6. SmbFile smbFile;
  7. try {
  8. // smb://userName:passWord@host/path/
  9. smbFile = new SmbFile(remoteUrl);
  10. if (!smbFile.exists()) {
  11. System.out.println("no such folder");
  12. } else {
  13. SmbFile[] files = smbFile.listFiles();
  14. for (SmbFile f : files) {
  15. System.out.println(f.getName());
  16. }
  17. }
  18. } catch (MalformedURLException e) {
  19. e.printStackTrace();
  20. } catch (SmbException e) {
  21. e.printStackTrace();
  22. }
  23. }

三、创建文件夹

  1. /**
  2. * 创建文件夹
  3. * @param remoteUrl
  4. * @param folderName
  5. * @return
  6. */
  7. public static void smbMkDir(String remoteUrl, String folderName) {
  8. SmbFile smbFile;
  9. try {
  10. // smb://userName:passWord@host/path/folderName
  11. smbFile = new SmbFile(remoteUrl + folderName);
  12. if (!smbFile.exists()) {
  13. smbFile.mkdir();
  14. }
  15. } catch (MalformedURLException e) {
  16. e.printStackTrace();
  17. } catch (SmbException e) {
  18. e.printStackTrace();
  19. }
  20. }

四、上传文件

  1. /**
  2. * 上传文件
  3. * @param remoteUrl
  4. * @param shareFolderPath
  5. * @param localFilePath
  6. * @param fileName
  7. */
  8. public static void uploadFileToSharedFolder(String remoteUrl, String shareFolderPath, String localFilePath, String fileName) {
  9. InputStream inputStream = null;
  10. OutputStream outputStream = null;
  11. try {
  12. File localFile = new File(localFilePath);
  13. inputStream = new FileInputStream(localFile);
  14. // smb://userName:passWord@host/path/shareFolderPath/fileName
  15. SmbFile smbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName);
  16. smbFile.connect();
  17. outputStream = new SmbFileOutputStream(smbFile);
  18. byte[] buffer = new byte[4096];
  19. int len = 0; // 读取长度
  20. while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
  21. outputStream.write(buffer, 0, len);
  22. }
  23. // 刷新缓冲的输出流
  24. outputStream.flush();
  25. } catch (FileNotFoundException e) {
  26. e.printStackTrace();
  27. } catch (MalformedURLException e) {
  28. e.printStackTrace();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. } finally {
  32. try {
  33. outputStream.close();
  34. inputStream.close();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }

五、下载文件

  1. /**
  2. * 下载文件到浏览器
  3. * @param httpServletResponse
  4. * @param remoteUrl
  5. * @param shareFolderPath
  6. * @param fileName
  7. */
  8. public static void downloadFileToBrowser(HttpServletResponse httpServletResponse, String remoteUrl, String shareFolderPath, String fileName) {
  9. SmbFile smbFile;
  10. SmbFileInputStream smbFileInputStream = null;
  11. OutputStream outputStream = null;
  12. try {
  13. // smb://userName:passWord@host/path/shareFolderPath/fileName
  14. smbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName);
  15. smbFileInputStream = new SmbFileInputStream(smbFile);
  16. httpServletResponse.setHeader("content-type", "application/octet-stream");
  17. httpServletResponse.setContentType("application/vnd.ms-excel;charset=UTF-8");
  18. httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + fileName);
  19. // 处理空格转为加号的问题
  20. httpServletResponse.setHeader("Content-Disposition", "attachment; fileName=" + fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"));
  21. outputStream = httpServletResponse.getOutputStream();
  22. byte[] buff = new byte[2048];
  23. int len;
  24. while ((len = smbFileInputStream.read(buff)) != -1) {
  25. outputStream.write(buff, 0, len);
  26. }
  27. } catch (MalformedURLException e) {
  28. e.printStackTrace();
  29. } catch (SmbException e) {
  30. e.printStackTrace();
  31. } catch (UnknownHostException e) {
  32. e.printStackTrace();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. finally {
  37. try {
  38. outputStream.close();
  39. smbFileInputStream.close();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  1. /**
  2. * 下载文件到指定文件夹
  3. * @param remoteUrl
  4. * @param shareFolderPath
  5. * @param fileName
  6. * @param localDir
  7. */
  8. public static void downloadFileToFolder(String remoteUrl, String shareFolderPath, String fileName, String localDir) {
  9. InputStream in = null;
  10. OutputStream out = null;
  11. try {
  12. SmbFile remoteFile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName);
  13. File localFile = new File(localDir + File.separator + fileName);
  14. in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
  15. out = new BufferedOutputStream(new FileOutputStream(localFile));
  16. byte[] buffer = new byte[1024];
  17. while (in.read(buffer) != -1) {
  18. out.write(buffer);
  19. buffer = new byte[1024];
  20. }
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. } finally {
  24. try {
  25. out.close();
  26. in.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }

六、删除文件

  1. /**
  2. * 删除文件
  3. * @param remoteUrl
  4. * @param shareFolderPath
  5. * @param fileName
  6. */
  7. public static void deleteFile(String remoteUrl, String shareFolderPath, String fileName) {
  8. SmbFile SmbFile;
  9. try {
  10. // smb://userName:passWord@host/path/shareFolderPath/fileName
  11. SmbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName);
  12. if (SmbFile.exists()) {
  13. SmbFile.delete();
  14. }
  15. } catch (MalformedURLException e) {
  16. e.printStackTrace();
  17. } catch (SmbException e) {
  18. e.printStackTrace();
  19. }
  20. }

删除文件夹将路径指向要删除的文件夹即可。

到此这篇关于java 通过 SmbFile 类操作共享文件夹的文章就介绍到这了,更多相关java操作共享文件夹内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/guorui_java/article/details/113619778

延伸 · 阅读

精彩推荐