Lately I’ve been playing with commons virtual file system
(commons-vfs), which is a really cool way to make your application file
system independent. You can write the same code to access files in your
local file system, samba servers, sftp servers, http, webdav,… see the complete list.
FileSystemManager fileSystemManager = VFS.getManager(); FileObject from = fileSystemManager.resolveFile(getFrom()); FileObject to = fileSystemManager.resolveFile(getTo(), getFileSystemOptions()); to.copyFrom(from, new AllFileSelector());
The application context with Spring would be something like the
following. You can see that setting the file system options it’s a pita
because they don’t follow the beans convention and you need to call
different methods instead of setting bean properties. I hop they change
ths in next versions.
<bean id="myBean" class="TestBean"> <property name="from" value="http://www.myserver.com/filename"/> <property name="to" value="sftp://username:password@myserver.com/home/whatever"/> <property name="fileSystemOptions" ref="sftpFileSystemOptions"/> </bean> <bean id="sftpFileSystemConfigBuilder" class="org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder" factory-method="getInstance"/> <bean id="sftpFileSystemOptions" class="org.apache.commons.vfs.FileSystemOptions"/> <bean id="sftpFileSystemOptions_disableStrictHostKeyChecking" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="sftpFileSystemConfigBuilder"/> <property name="targetMethod" value="setStrictHostKeyChecking"/> <property name="arguments"> <list> <ref local="sftpFileSystemOptions"/> <value>no</value> </list> </property> </bean> <!-- use private key instead of password --> <bean id="sftpFileSystemOptions_setIdentities" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="sftpFileSystemConfigBuilder"/> <property name="targetMethod" value="setIdentities"/> <property name="arguments"> <list> <ref local="sftpFileSystemOptions"/> <list><value>Path to the ssh identity file</value></list> </list> </property> </bean>
Another drawback is that you need to build from sources, no releases are available yet 😦