package com.ecc.liana.innermanage.file;
import java.io.File;
import java.io.FileInputStream;
import org.apache.log4j.Logger;
import com.ecc.emp.core.Context;
import com.ecc.emp.core.EMPException;
import com.ecc.liana.base.LianaAction;
import com.ecc.liana.base.LianaConstants;
import com.ecc.liana.base.LianaStandard;
import com.ecc.liana.exception.TranFailException;
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;
/**
* 将文件通过SFTP方式上传到FTP服务器的Action<br>
*
* @version 1.0 2013-05-03
* @author wangxx@yuchengtech.com
*/
public class SftpUploadAction extends LianaAction {
Session session = null;
Channel channel = null;
/**
* FTP 数据传输超时时间
*/
private int timeout = 0;
/**
* 保存本地文件名称
*/
private String localFileNameField;
private static final Logger LOG = Logger.getLogger(SftpUploadAction.class.getName());
@Override
public String execute(Context context) throws EMPException {
// TODO Auto-generated method stub
ChannelSftp sftp = null;
try{
String fileName = (String)context.getDataValue(localFileNameField);
String localPath = LianaStandard.getSelfDefineSettingsValue("localFilePath");
String remotepath = LianaStandard.getSelfDefineSettingsValue("remoteFilePath");
String sftpHost = LianaStandard.getSelfDefineSettingsValue("SftpHost");
String port = LianaStandard.getSelfDefineSettingsValue("SftpPort");
String sftpUserName = LianaStandard.getSelfDefineSettingsValue("SftpUserName");
String sftpPassword = LianaStandard.getSelfDefineSettingsValue("SftpPassword");
String sftpTimeout = LianaStandard.getSelfDefineSettingsValue("SftpTimeout");
if (sftpTimeout != null && !sftpTimeout.equals("")) {
timeout = Integer.valueOf(sftpTimeout);
}
int sftpPort = 22;
if (port != null && !port.equals("")) {
sftpPort = Integer.valueOf(port);
}
JSch jsch = new JSch(); // 创建JSch对象
session = jsch.getSession(sftpUserName, sftpHost, sftpPort); // 根据用户名,主机ip,端口获取一个Session对象
LOG.debug("Session created.");
if (sftpPassword != null) {
session.setPassword(sftpPassword); // 设置密码
}
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(timeout); // 设置timeout时间
session.connect(); // 通过Session建立链接
LOG.debug("Session connected.");
LOG.debug("Opening Channel.");
channel = session.openChannel("sftp"); // 打开SFTP通道
channel.connect(); // 建立SFTP通道的连接
sftp = (ChannelSftp) channel;
LOG.debug("Connected successfully to ftpHost = " + sftpHost + ",as ftpUserName = " + sftpUserName
+ ", returning: " + channel);
LOG.debug("开始上传图片文件-->>"+fileName);
uploadBySftp(remotepath,localPath+fileName,sftp);
LOG.debug(fileName+" 上传成功......");
}catch(JSchException sex){
LOG.error("SftpUploadAction.execute() 文件上传...上传失败:"+sex.getMessage());
// context.setDataValue("errorCode", "EBIM1801");
throw new TranFailException( "EBIM1801","文件上传失败");
}catch(Exception e){
LOG.error("SftpUploadAction.execute() 文件上传...执行失败:"+e.getMessage());
// context.setDataValue("errorCode", "EBIM1802");
throw new TranFailException( "EBIM1802","文件上传执行失败");
}finally{
releaseResource( session , channel );
}
return DEFAULT_RETURN_VALUE;
}
/**
* 上传文件
* @param directory 上传的目录
* @param uploadFile 要上传的文件
* @param sftp
*/
public void uploadBySftp(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file=new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @desc 释放资源
* @param session
* @param channel
*/
protected void releaseResource(Session session,Channel channel) {
if (channel != null) {
channel.disconnect();
LOG.debug("channel released...");
}
if (session != null) {
session.disconnect();
LOG.debug("session realsed...");
}
}
public void setLocalFileNameField(String localFileNameField) {
this.localFileNameField = localFileNameField;
}
}