在項目中準(zhǔn)備替換ActiveMQ的http方式為sftp方式下載文件梅猿。由于sftp需要開通22端口待错,也就是說ssh也能用了嘶卧,因此也可以通過ssh執(zhí)行一些命令亲善。
需要引用jsch-0.1.44.jar
首先直接上sftp的代碼。
package com.icbc.SFTP;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class MediaSftp {
public Session session;
private String usr;
private String pwd;
public MediaSftp(String usr,String pwd,String ip) throws JSchException {
this.usr = usr;
this.pwd=pwd;
Connect(ip);
}
public void Connect(String ip) throws JSchException{
JSch jsch = new JSch();
try{
this.session = jsch.getSession(this.usr, ip,22);
if(session != null){
session.setPassword(this.pwd);
session.setConfig("StrictHostKeyChecking","no");
session.setTimeout(30000);
session.connect();
System.out.println("connect to "+ip);
if(session != null && !session.isConnected()){
this.close();
}
}
}catch(JSchException e){
throw e;
}
}
public void close(){
if(session != null && session.isConnected()){
session.disconnect();
// this.isConnectSSH = false;
}else{
}
}
public boolean download(String downloadFile,String saveFile) throws Exception{
try {
ChannelSftp sftp = (ChannelSftp)
session.openChannel("sftp");
sftp.connect();
sftp.cd("/opt/activemq/apache-activemq-5.13.4/webapps/fileserver/media/");
File file = new File(saveFile);
System.out.println(saveFile);
File parentFile = file.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
InputStream in = null;
FileOutputStream outputStream = new FileOutputStream(file);
in = sftp.get(downloadFile);
//可通過byte數(shù)組長度控制下載速度
byte[] buffer = new byte[20480];
int count = 0;
while ((count = in.read(buffer)) != -1) {
if (count == 0) break;
outputStream.write(buffer, 0, count);
}
in.close();
outputStream.close();
outputStream.flush();
sftp.disconnect();
this.close();
return true;
} catch (Exception e) {
throw e;
}
}
}
調(diào)用時先實例化這個類,然后直接調(diào)用download方法就可以了澎灸。入?yún)⑹且螺d的文件名院塞,和本地要保存的路徑,然后拼成本地文件名傳入download方法性昭。返回值是下載耗時拦止。
public long getMedia(String hostip,String fileName, String localPath) throws Exception {
MediaSftp sftp = new MediaSftp("username","passwords",hostip);
String localFile = localPath+fileName;
long start = System.currentTimeMillis();
sftp.download(fileName, localFile);
return System.currentTimeMillis()-start;
}
此外,jsch還提供了遠(yuǎn)程ssh協(xié)議登陸后執(zhí)行命令的方法糜颠。下面是一個執(zhí)行命令獲取目錄中最近更新的文件名的方法汹族,可以把這個方法添加到MediaSftp類里,實例化MedisSftp類建立連接后其兴,就可以調(diào)用這個方法了顶瞒。
public String getLatestFileName(String patterns) throws Exception {
//執(zhí)行的命令,這里為查找目錄下最近更新的文件名帶有patterns的文件
String cmd = "ls -ltr /opt/activemq/apache-activemq-5.13.4/webapps/fileserver/media/|grep "+patterns+"|tail -1|awk {'print $NF'}\n";
ByteArrayOutputStream retOut = new ByteArrayOutputStream();
ChannelShell channelShell = (ChannelShell)session.openChannel("shell");
PipedInputStream cmdIn = new PipedInputStream();
PipedOutputStream cmdOut = new PipedOutputStream(cmdIn);
channelShell.setInputStream(cmdIn);
channelShell.setOutputStream(retOut);
channelShell.connect(30000);
cmdOut.write(cmd.getBytes());
cmdOut.flush();
//緩沖時間和執(zhí)行時間
Thread.sleep(2000);
cmdOut.close();
cmdIn.close();
String retMsg = retOut.toString();
retOut.close();
channelShell.disconnect();
//得到的結(jié)果包含很多行忌警,需要處理
String[] retArr= retMsg.split("\n");
return retArr[retArr.length-2].trim();
}