使用VFS进行SFTP网络传输----转载

来源:互联网 发布:php cookie存储数组 编辑:程序博客网 时间:2024/04/30 13:17

 /**
 * 转载
 */
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:xxxx@192.168.1.16/doc-root/myaoVfstest/");

  }

 

原创粉丝点击