I needed to connect, with authentication, to a SAMBA shared drive to download and process data files for one of my applications. Thinking VFS2 would do the job, I had a go at it.Results were good, so now documented here:
private static void GetFiles() throws IOException { jcifs.Config.registerSmbURLHandler(); NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication( prop.getProperty("smbDomain"), prop.getProperty("smbUser"), prop.getProperty("smbPass")); StaticUserAuthenticator authS = new StaticUserAuthenticator( prop.getProperty("smbDomain"), prop.getProperty("smbUser"), prop.getProperty("smbPass")); FileSystemOptions opts = new FileSystemOptions(); DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, authS); SmbFile smbFile = new SmbFile(prop.getProperty("smbURL"),auth); FileSystemManager fs = VFS.getManager(); String[] files = smbFile.list(); for(String file:files) { SmbFile remFile = new SmbFile(prop.getProperty("smbURL") + file, auth); SmbFileInputStream smbfos = new SmbFileInputStream(remFile); OutputStream out = new FileOutputStream(file); byte[] b = new byte[8192]; int n; while ((n = smbfos.read(b)) > 0) { out.write(b, 0, n); } smbfos.close(); out.close(); } }
As you can see from the above, this simply copies the files to local, preserving the filename, which is exactly what I want. If you need to write files, it is very similar, just use the SmbFileOutputStream instead!
Nice! VFS2 comes through once again!