如何使用Java SmbFile类高效管理企业级网络共享文件夹?
- 内容介绍
- 相关推荐
本文共计523个文字,预计阅读时间需要3分钟。
一、添加依赖在pom.xml文件中添加以下依赖即可使用SmbFile类:xml jcifs jcifs 1.3.17
二、读取文件javaimport jcifs.smb.SmbFile;import java.io.IOException;
public class SmbFileExample { public static void main(String[] args) { String smbPath=smb://server/share/folder/file.txt; try { SmbFile file=new SmbFile(smbPath); if (file.exists()) { // 读取文件内容 System.out.println(file.getContent()); } else { System.out.println(文件不存在); } } catch (IOException e) { e.printStackTrace(); } }}
一、添加依赖
在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 userName:passWord@host/path/shareFolderPath/fileName smbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName); smbFileInputStream = new SmbFileInputStream(smbFile); 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操作共享文件夹内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计523个文字,预计阅读时间需要3分钟。
一、添加依赖在pom.xml文件中添加以下依赖即可使用SmbFile类:xml jcifs jcifs 1.3.17
二、读取文件javaimport jcifs.smb.SmbFile;import java.io.IOException;
public class SmbFileExample { public static void main(String[] args) { String smbPath=smb://server/share/folder/file.txt; try { SmbFile file=new SmbFile(smbPath); if (file.exists()) { // 读取文件内容 System.out.println(file.getContent()); } else { System.out.println(文件不存在); } } catch (IOException e) { e.printStackTrace(); } }}
一、添加依赖
在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 userName:passWord@host/path/shareFolderPath/fileName smbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName); smbFileInputStream = new SmbFileInputStream(smbFile); 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操作共享文件夹内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

