Java 호출 직접 읽기 및 쓰기 Samba 서버

1925 단어
자세히 보기
 
	/**
	 *  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}/

좋은 웹페이지 즐겨찾기