将文件通过SFTP方式上传到FTP服务器的Action<br>

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;
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值