一、 功能介绍
通过在o2服务管理下的代理服务或接口服务中实现sftp上传。
二、以接口服务为例
从菜单进入应用-服务管理-接口配置,新建一个接口代码如下:
/********************
this.entityManager; //实体管理器
this.applications; //访问系统内服务
this.requestText//请求正文
this.request//请求
this.currentPerson//当前用户
this.response//响应对象。通过this.response.setBody(data)设置响应内容
this.organization; //组织访问
this.org; //组织快速访问方法
this.service; //webSerivces客户端
********************/
try {
varStandardFileSystemManager = Java.type('org.apache.commons.vfs2.impl.StandardFileSystemManager');
var manager = newStandardFileSystemManager();
manager.init();
print("ok---------------1")
varNameValuePair = Java.type('org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder');
var sftpBuilder = NameValuePair.getInstance();
varFileSystemOptions = Java.type('org.apache.commons.vfs2.FileSystemOptions');
var opts = newFileSystemOptions();
sftpBuilder.setStrictHostKeyChecking(opts, "no");
sftpBuilder.setUserDirIsRoot(opts, true);
sftpBuilder.setTimeout(opts, 10000);
print("ok---------------2")
//sftp用户名
var sftpuser = "root";
//sftp密码
var sftppassword = "xxxxx";
//sftp主机ip
var sftphost = "xxxxxx";
//sftp远程目录
var remoteDownLoadPath = "/home/taizhangsftp/history.json";
//sftp本地目录
var localDires = "/data/o2server/local/temp/history.json";
var sftpUri = "sftp://"+sftpuser+":"+sftppassword+"@"+sftphost+remoteDownLoadPath;
print("sftpUri="+sftpUri);
varFile = Java.type('java.io.File');
var fileObj = newFile(localDires);
var localFile = manager.resolveFile(fileObj.getAbsolutePath());
var remoteFile = manager.resolveFile(sftpUri, opts);
varSelectors = Java.type('org.apache.commons.vfs2.Selectors');
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
print("ok---------------3");
} catch (e) {
print("sftp上传错误");
print(e);
}
"xxxx";
三、运行接口
运行接口结果如图: