Samba服务器文件夹使用SMB协议,如何实现高效的上传、下载和复制操作?

更新于
2026-10-12 09:46:13
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计387个文字,预计阅读时间需要2分钟。

Samba服务器文件夹使用SMB协议,如何实现高效的上传、下载和复制操作?

复制以下代码:

javapublic static boolean copyFolder(String oldPath, String newPath) { try { File file=new File(newPath); if (!file.exists()) { file.mkdir(); } SmbFile oldFile=new SmbFile(oldPath); String[] files=oldFile.list(); SmbFile temp=null; for (

复制

public static boolean copyFolder(String oldPath, String newPath) { try { File file = new File(newPath); if (!file.exists()) { file.mkdir(); } SmbFile oldFile = new SmbFile(oldPath); String[] files = oldFile.list(); SmbFile temp = null; for (int i = 0; i < files.length; i++) { if (oldPath.endsWith("/")) { temp = new SmbFile(oldPath + files[i]); } else { temp = new SmbFile(oldPath + File.separator + files[i]); } if (temp.isFile()) { InputStream input =new BufferedInputStream( new SmbFileInputStream(temp)); OutputStream output =new BufferedOutputStream(new FileOutputStream(newPath + File.separator + (temp.getName()).toString())); byte[] b = new byte[1024 * 5000]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); b = new byte[1024 * 5000]; System.out.println("导入中。。。"); } output.close(); input.close(); } if (temp.isDirectory()) { copyFolder(oldPath + files[i] + "/", newPath + File.separator + files[i]); } } return true; } catch (Exception ex) { ex.printStackTrace(); return false; } } 本地磁盘上传到服务器

public static void smbPut(String remote_Url, String localFilePath) { InputStream in = null; OutputStream out = null; try { File localFile = new File(localFilePath); String fileName = localFile.getName(); SmbFile remoteFile = new SmbFile(remote_Url + "/" + fileName); in = new BufferedInputStream(new FileInputStream(localFile)); out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); byte[] buffer = new byte[BUFFER_SIZE]; while ((in.read(buffer)) != -1) { out.write(buffer); buffer = new byte[1024]; System.out.println("上传成功....."); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } } 服务端下载到本地磁盘

public static void smbGet(String remoteUrl, String localDir) { InputStream in = null; OutputStream out = null; try { SmbFile smbFile = new SmbFile(remoteUrl); String fileName = smbFile.getName(); File localFile = new File(localDir + File.separator + fileName); in = new BufferedInputStream(new SmbFileInputStream(smbFile)); out = new BufferedOutputStream(new FileOutputStream(localFile)); byte[] buffer = new byte[BUFFER_SIZE]; while ((in.read(buffer)) != -1) { out.write(buffer); buffer = new byte[1024]; System.out.println("下载成功....."); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } }

Samba服务器文件夹使用SMB协议,如何实现高效的上传、下载和复制操作?
标签:上传

本文共计387个文字,预计阅读时间需要2分钟。

Samba服务器文件夹使用SMB协议,如何实现高效的上传、下载和复制操作?

复制以下代码:

javapublic static boolean copyFolder(String oldPath, String newPath) { try { File file=new File(newPath); if (!file.exists()) { file.mkdir(); } SmbFile oldFile=new SmbFile(oldPath); String[] files=oldFile.list(); SmbFile temp=null; for (

复制

public static boolean copyFolder(String oldPath, String newPath) { try { File file = new File(newPath); if (!file.exists()) { file.mkdir(); } SmbFile oldFile = new SmbFile(oldPath); String[] files = oldFile.list(); SmbFile temp = null; for (int i = 0; i < files.length; i++) { if (oldPath.endsWith("/")) { temp = new SmbFile(oldPath + files[i]); } else { temp = new SmbFile(oldPath + File.separator + files[i]); } if (temp.isFile()) { InputStream input =new BufferedInputStream( new SmbFileInputStream(temp)); OutputStream output =new BufferedOutputStream(new FileOutputStream(newPath + File.separator + (temp.getName()).toString())); byte[] b = new byte[1024 * 5000]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); b = new byte[1024 * 5000]; System.out.println("导入中。。。"); } output.close(); input.close(); } if (temp.isDirectory()) { copyFolder(oldPath + files[i] + "/", newPath + File.separator + files[i]); } } return true; } catch (Exception ex) { ex.printStackTrace(); return false; } } 本地磁盘上传到服务器

public static void smbPut(String remote_Url, String localFilePath) { InputStream in = null; OutputStream out = null; try { File localFile = new File(localFilePath); String fileName = localFile.getName(); SmbFile remoteFile = new SmbFile(remote_Url + "/" + fileName); in = new BufferedInputStream(new FileInputStream(localFile)); out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); byte[] buffer = new byte[BUFFER_SIZE]; while ((in.read(buffer)) != -1) { out.write(buffer); buffer = new byte[1024]; System.out.println("上传成功....."); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } } 服务端下载到本地磁盘

public static void smbGet(String remoteUrl, String localDir) { InputStream in = null; OutputStream out = null; try { SmbFile smbFile = new SmbFile(remoteUrl); String fileName = smbFile.getName(); File localFile = new File(localDir + File.separator + fileName); in = new BufferedInputStream(new SmbFileInputStream(smbFile)); out = new BufferedOutputStream(new FileOutputStream(localFile)); byte[] buffer = new byte[BUFFER_SIZE]; while ((in.read(buffer)) != -1) { out.write(buffer); buffer = new byte[1024]; System.out.println("下载成功....."); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } }

Samba服务器文件夹使用SMB协议,如何实现高效的上传、下载和复制操作?
标签:上传