使用VFS进行sftp传输
偶然的机会了解啦VFS,在研究的过程中不得不对其功能的强大感到叹服,以下虽然是摘抄的代码,但可以作为一个简单的例子,呵呵,其它功能正在研究中。。。
package com.meetexpo.showcase.monitor; import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.FileSystemOptions; import org.apache.commons.vfs.Selectors; import org.apache.commons.vfs.cache.DefaultFilesCache; import org.apache.commons.vfs.impl.DefaultFileSystemManager; import org.apache.commons.vfs.provider.local.DefaultLocalFileProvider; import org.apache.commons.vfs.provider.sftp.SftpFileProvider; import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder; import org.apache.commons.vfs.provider.zip.ZipFileProvider; /** * @author myao Update:2006-7-1810:40:46 */ public class VfsOp { private String _sourceroot = "C:/vfsroot"; private String _targetroot = "sftp://xxx:xxx@xxxx/doc-root/myaoVfstest/"; // b1:f1:ef:26:3e:5f:a5:0d:70:fa:5e:df:d9:6b:55:41 private FileObject localfs, targetfs; private DefaultFileSystemManager vfsmgr; VfsOp() { try { init(); } catch (FileSystemException e) { e.printStackTrace(); } } void init() throws FileSystemException { vfsmgr = getDefaultFileSystemManager(); } void moveFile(String sourcePath, String targetPath) throws FileSystemException { localfs = vfsmgr.resolveFile(sourcePath); if (!localfs.exists()) { localfs.createFolder(); // localfs. } // vfsmgr. FileSystemOptions opts = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking( opts, "no"); targetfs = vfsmgr.resolveFile(targetPath, opts); if (!targetfs.exists()) { targetfs.createFolder(); } try { long startTime = System.currentTimeMillis(); // System.out.println("Source File:" + source.getChildren().length); targetfs.copyFrom(localfs, Selectors.SELECT_FILES); // System.out.println("Target File:" + target.getChildren().length); // TODO 要想办法用遍历的方法来拷贝文件,否则无法留下拷贝的细节。 正在装载数据…… // FileObject[] flist = localfs.getChildren(); // for (int i = 0; i < flist.length; i++) { // FileObject tmp = vfsmgr.resolveFile(targetfs, flist[i].getName() // .getBaseName()); // if (!tmp.exists()) { // tmp.createFile(); // } // // VfsMutiMove t = new VfsMutiMove(flist[i], tmp); // t.run(); // // tmp.copyFrom(flist[i], Selectors.SELECT_SELF); // } long endTime = System.currentTimeMillis(); System.out.println(this.getClass().getName()); System.out.println("Cost time(ms:):" + (endTime - startTime)); } catch (FileSystemException e) { e.printStackTrace(); } } private DefaultFileSystemManager getDefaultFileSystemManager() { DefaultFileSystemManager mgr = new DefaultFileSystemManager(); // SFTP 供应者 SftpFileProvider fp = new SftpFileProvider(); FileSystemOptions t = new FileSystemOptions(); // ZIP 供应者 ZipFileProvider zp = new ZipFileProvider(); // 缺省本地文件供应者 DefaultLocalFileProvider lf = new DefaultLocalFileProvider(); try { // common-vfs 中 文件管理器的使用范例 mgr.addProvider("sftp", fp); mgr.addProvider("zip", zp); mgr.addProvider("file", lf); mgr.setFilesCache(new DefaultFilesCache()); mgr.init(); } catch (FileSystemException e) { // 此处应该改为log e.printStackTrace(); } return mgr; } void getWorkspaceFromProperties() { /* * Properties tmpProperties; File tmpfile = new * File("workspace.properties"); */ } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { VfsOp op = new VfsOp(); op.moveFile("C:/downloads", "sftp://xxxx:[email protected]/doc-root/myaoVfstest/"); } }
相关推荐
hickwu 2020-10-23
杨立峰 2020-08-18
diediexiaomi 2020-08-11
wq0 2020-07-18
leodengzx 2020-06-28
liyansring 2020-06-21
jiahaohappy 2020-06-21
leodengzx 2020-06-14
xsg 2020-06-09
piaotiejun 2020-06-07
TangCuYu 2020-06-05
clmngu 2020-06-03
河的第三条岸 2020-05-31
atb 2020-05-30
书弋江山 2020-05-30
LWLWLiang 2020-05-28
JJandYY 2020-05-25
饮马天涯 2020-05-20
wennuanwarm 2020-05-16