当前位置 : 主页 > 编程语言 > c++ >

基于smb协议对Samba服务器文件夹的上传、下载、复制

来源:互联网 收集:自由互联 发布时间:2021-07-03
复制 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 (
复制
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();
			}
		}
	}
网友评论