SFTPUtils.java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SFTPUtils {
private static final Log log = LogFactory.getLog(SFTPUtils.class);
/**
* 通过密码SFTP连接服务器
*
* @throws Exception
*/
public Session connectWithPassword(String username,String passwd,String address,int port) throws Exception {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, address, port);
// if (log.isInfoEnabled()) {
// log.info("Session 已创建.");
// }
session.setPassword(passwd);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect();
return session;
} catch (Exception e) {
throw new Exception("创建sftp连接异常", e);
}
}
public ChannelSftp getChannelSftp(Session session){
Channel channel = null;
try {
channel = session.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
System.err.println("创建sftp连接异常");
}
return (ChannelSftp) channel;
}
public void disconnect(ChannelSftp channelSftp,Session session) {
if (channelSftp != null) {
if (channelSftp.isConnected()) {
channelSftp.disconnect();
}
}
if (session != null) {
if (session.isConnected()) {
session.disconnect();
}
}
}
/**
* * 下载单个文件
*
* @param remotPath
* 远程下载目录(以路径符号结束)
* @param remoteFileName
* 下载文件名
* @param localPath
* 本地保存目录(以路径符号结束)
* @param localFileName
* 保存文件名
* @return
*/
public boolean downloadFile(ChannelSftp channelSftp,String remotePath, String remoteFileName, String localPath, String localFileName) {
FileOutputStream fileoutput = null;
String remoteFile = "";
File file = null;
try {
String localFile = localPath + File.separator + localFileName;
remoteFile = remotePath + File.separator + remoteFileName;
file = new File(localFile);
mkdirs(localFile);
fileoutput = new FileOutputStream(file);
if(log.isInfoEnabled()){
log.info("localFile : " + localFile + ",remoteFile : " + remoteFile);
}
channelSftp.get(remoteFile, fileoutput);
return true;
} catch(SftpException e){
if(e.toString().equals("NO_FILE")){
System.out.println("下载文件缺失");
}
if(log.isErrorEnabled()){
log.error(e.getMessage());
}
return false;
}catch (Exception e) {
file.delete();
if(log.isErrorEnabled()){
log.error(e.getMessage());
}
return false;
} finally {
if (null != fileoutput) {
try {
fileoutput.close();
} catch (IOException e) {
}
}
}
}
/**
* 如果目录不存在就创建目录
*
* @param path
*/
private void mkdirs(String path) {
File f = new File(path);
String fs = f.getParent();
f = new File(fs);
if (!f.exists()) {
f.mkdirs();
}
}
}
测试代码
SFTPUtils sftpUtil = new SFTPUtils();
Session session = null;
ChannelSftp channelSftp = null;
String systemId = context.getDelocalizedValue(ContextConstants.REAL_SYSTEM);
String idmsPath = localPath + File.separator + "FileGateWay" + File.separator + systemId;
try {
session = sftpUtil.connectWithPassword(userName, passwd, address, port);
channelSftp = sftpUtil.getChannelSftp(session);
boolean sftpResult = sftpUtil.downloadFile(channelSftp, remotePath,remoteFileName, idmsPath, remoteFileName);
if(sftpResult){
log.info("下载文件成功");
}else{
log.error("下载文件失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("下载文件失败");
}finally{
sftpUtil.disconnect(channelSftp, session);
}