`
jiaoronggui
  • 浏览: 1304069 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
B7c2eb31-a8ea-3973-a517-d00141f39b89
项目管理软件-redmin...
浏览量:115275
4a63e153-250f-30f6-a051-97cfc67cb3d3
IT职业规划
浏览量:197745
社区版块
存档分类
最新评论

Java调用直接读写samba服务器

    博客分类:
  • Java
 
阅读更多

 

	/**
	 * 同步文件到smb服务器,一共四台samba服务器
	 * @param files 同步的文件列表
	 * @param ctx
	 */
	public static void _fileSync(String[] files,String SmbUrl){
		try{

			
			SmbFile remote = new SmbFile(SmbUrl);

			for (int i = 0; i < files.length; i++) {
				File file = new File(files[i]);
				String filePath = file.getAbsolutePath();
				//System.out.println(filePath);
	
				// 同步到文件服务器
				System.out.println(Constants.REALPATH);
				String smbFilePath = filePath.substring(Constants.REALPATH.length()+1);
				//System.out.println(smbFilePath);
				String smbFolderPath = smbFilePath.substring(0, smbFilePath.lastIndexOf(File.separator));
				//System.out.println(smbFolderPath);
				//System.out.println(smbFolderPath.replace('\\', '/'));
				SmbFile remoteFolder = new SmbFile(remote, smbFolderPath.replace('\\', '/'));
				//System.out.println(remote);
				//System.out.println(remoteFolder);
				if (!remoteFolder.exists()) {
					remoteFolder.mkdirs();
				}
								
				SmbFile remoteFile = new SmbFile(remote, smbFilePath);
	
				
				if (!remoteFile.exists()) {
					remoteFile.createNewFile();
				}


				FileInputStream fis = new FileInputStream(file);
				SmbFileOutputStream sfos = new SmbFileOutputStream(remoteFile);

				int c;
				byte[] buf = new byte[2048];
				while ((c = fis.read(buf)) != -1) {
					sfos.write(buf, 0, c);

				}
				sfos.flush();

				fis.close();
	
				// 删除本地文件
				file.delete();
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

 

其中SmbUrl格式是

smb://{username}:{passwd}@{ip}/{smbshare}/

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics