import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javautilProperties;
import comjcraftjschChannel;
import comjcraftjschChannelSftp;
import comjcraftjschJSch;
import comjcraftjschSession;
import comjcraftjschSftpException;
import hkrtb2bviewutilLog;
import javautilVector;
import znccfccbutilCCFCCBUtil;
/
/
public class CCFCCBSftp {
/
连接sftp服务器
@return
/
public static ChannelSftp connect() {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jschgetSession(CCFCCBUtilCCFCCBHOSTNAME, CCFCCBUtilCCFCCBHOSTNAME, 22);
Session sshSession = jschgetSession(CCFCCBUtilCCFCCBLOGINNAME, CCFCCBUtilCCFCCBHOSTNAME, 22);
Systemoutprintln("Session created");
sshSessionsetPassword(CCFCCBUtilCCFCCBLOGINPASSWORD);
Properties sshConfig = new Properties();
sshConfigput("StrictHostKeyChecking", "no");
sshSessionsetConfig(sshConfig);
sshSessionconnect();
Systemoutprintln("Session connected");
Systemoutprintln("Opening Channel");
Channel channel = sshSessionopenChannel("sftp");
channelconnect();
sftp = (ChannelSftp) channel;
Systemoutprintln("Connected to " + CCFCCBUtilCCFCCBHOSTNAME + "");
} catch (Exception e) {
}
return sftp;
}
/
连接sftp服务器
@param host 主机
@param port 端口
@param username 用户名
@param password 密码
@return
/
public static ChannelSftp connect(String host, int port, String username,
String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jschgetSession(CCFCCBUtilCCFCCBHOSTNAME, CCFCCBUtilCCFCCBHOSTNAME, 22);
Session sshSession = jschgetSession(CCFCCBUtilCCFCCBLOGINNAME, host, port);
Systemoutprintln("Session created");
sshSessionsetPassword(CCFCCBUtilCCFCCBLOGINPASSWORD);
Properties sshConfig = new Properties();
sshConfigput("StrictHostKeyChecking", "no");
sshSessionsetConfig(sshConfig);
sshSessionconnect();
Systemoutprintln("Session connected");
Systemoutprintln("Opening Channel");
Channel channel = sshSessionopenChannel("sftp");
channelconnect();
sftp = (ChannelSftp) channel;
Systemoutprintln("Connected to " + host + "");
} catch (Exception e) {
}
return sftp;
}
/
上传文件
@param directory 上传的目录
@param uploadFile 要上传的文件
@param sftp
/
public void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftpcd(directory);
File file = new File(uploadFile);
sftpput(new FileInputStream(file), filegetName());
} catch (Exception e) {
eprintStackTrace();
}
}
/
下载文件
@param directory 下载目录
@param downloadFile 下载的文件
@param saveFile 存在本地的路径
@param sftp
@return
/
public static String download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {
try {
sftpcd(directory);
File file = new File(saveFile);
FileOutputStream fos = new FileOutputStream(file);
sftpget(downloadFile, fos);
sftpdisconnect();
fosclose();
} catch (Exception e) {
Loginfo("下载文件过程出错:" + egetMessage());
return "false";
}
return "true";
}
/
删除文件
@param directory 要删除文件所在目录
@param deleteFile 要删除的文件
@param sftp
/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftpcd(directory);
sftprm(deleteFile);
} catch (Exception e) {
}
}
/
列出目录下的文件
@param directory 要列出的目录
@param sftp
@return
@throws SftpException
/
public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException {
return sftpls(directory);
}
public static void main(String[] args) {
CCFCCBSftp sf = new CCFCCBSftp();
String host = CCFCCBUtilCCFCCBHOSTNAME;
int port = 22;
String username = CCFCCBUtilCCFCCBLOGINNAME;
String password = CCFCCBUtilCCFCCBLOGINPASSWORD;
String directory = "/ccfccb/904999900099/download/";
//String uploadFile = "D:\\tmp\\uploadtxt";
String downloadFile = "CCF_904999900099_20150317_0001zip";
String saveFile = CCFCCBUtilCCFCCBUploadFilePath + "//" + "CCF_904999900099_20150317_0001zip";
//String deleteFile = "deletetxt";
ChannelSftp sftp = CCFCCBSftpconnect(host, port, username, password);
//sfupload(directory, uploadFile, sftp);
CCFCCBSftpdownload(directory, downloadFile, saveFile, sftp);
//sfdelete(directory, deleteFile, sftp);
try {
sftpcd(directory);
// sftpmkdir("ss");
Systemoutprintln("finished");
} catch (Exception e) {
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)