使用VFS进行SFTP网络传输----转载
2006-07-25 10:21
525 查看
/**
* 转载
*/
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/");
}
}
* 转载
*/
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/");
}
}
相关文章推荐
- Java使用udp传输方式进行网络通信
- LINUX: ftp,sftp,scp网络传输命令配置及使用
- [置顶] 使用protobuff 进行网络序列化和反序列传输实例
- 使用SecureCRT的SFTP在WINDOWS与LINUX之间传输文件(转载)
- 减小网络传输图片大小,使用gzip进行压缩
- 转载--Ajax学习---使用 JSON 进行数据传输
- linux上使用ftp,sftp,rsync,scp,套接字 来通过网络传输文件
- 在android需要大量使用xml进行网络传输时,如何用面向对象的思路降低xml的耦合性并增加重用性
- Linux使用SFTP进行数据传输
- 贴别人代码的坏处:使用io流进行网络传输
- 邮件安全之使用PGP软件对邮件信息进行加密和签名(转载)
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式(转)
- SSM框架下使用JSON进行前后端数据传输
- Apache2.4使用require指令进行访问控制--允许或限制IP访问/通过User-Agent禁止不友好网络爬虫
- swift 使用Kingfisher进行网络图片的下载
- 使用 JSON 进行数据传输
- ab和jmeter进行GET/POST压力测试的使用心得和比较(转载,在ubuntu16.04下使用ab带json对centos7压力测试验证可用)
- Ajax学习摘录之第十部分 使用 JSON 进行数据传输
- [转]使用 Twisted Matrix 框架来进行网络编程 .
- 服务器使用Gzip压缩数据,加快网络传输(Java 例子)