解决JAVA实现FTP连接服务器Server Reply: SSH-2.0-OpenSSH_6.6.1异常的问题

客户需求要求写一个定时任务每天定时从服务器获取数据文件解析,在使用FTP连接服务器时报错

org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-OpenSSH_6.6.1

原因是FTPClient不支持通过协议SSH2进行SFTP连接,只能更换方法实现,可以使用com.jcraft.jsch.JSch提供的SSH解决问题。

  1. 添加maven依赖:
<dependency>
	<groupId>com.jcraft</groupId>
	<artifactId>jsch</artifactId>
	<version>0.1.54</version>    
</dependency>

非maven项目自行查找对应jar包添加进项目。

  1. 编写工具类实现定时器调用,代码如下:
package cn.com.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class FtpUtil {
	private static final Logger loggerMonitor = LoggerFactory.getLogger("monitor");

	/**
	 * FTPClient对象
	 **/
	private static ChannelSftp ftpClient = null;
	/**
	 *
	 */
	private static Session sshSession = null;

	/**
	 * 连接服务器
	 * @param host
	 * @param port
	 * @param userName
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public static ChannelSftp getConnect(String host, String port, String userName, String password)
			throws Exception {
		try {
			JSch jsch = new JSch();
			// 获取sshSession
			sshSession = jsch.getSession(userName, host, Integer.parseInt(port));
			// 添加s密码
			sshSession.setPassword(password);
			Properties sshConfig = new Properties();
			sshConfig.put("StrictHostKeyChecking", "no");
			sshSession.setConfig(sshConfig);
			// 开启sshSession链接
			sshSession.connect();
			// 获取sftp通道
			ftpClient = (ChannelSftp) sshSession.openChannel("sftp");
			// 开启
			ftpClient.connect();
			loggerMonitor.debug("success ..........");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("连接sftp服务器异常。。。。。。。。");
		}
		return ftpClient;
	}

	/**
	 * 下载文件
	 * @param ftp_path	服务器文件路径
	 * @param save_path	下载保存路径
	 * @param oldFileName	服务器上文件名
	 * @param newFileName	保存后新文件名
	 * @throws Exception
	 */
	public static void download(String ftp_path, String save_path, String oldFileName, String newFileName)
			throws Exception {
		FileOutputStream fos = null;
		try {
			ftpClient.cd(ftp_path);
			File file = new File(save_path);
			if (!file.exists()) {
				file.mkdirs();
			}
			String saveFile = save_path + newFileName;
			File file1 = new File(saveFile);
			fos = new FileOutputStream(file1);
			ftpClient.get(oldFileName, fos);
		} catch (Exception e) {
			loggerMonitor.error("下载文件异常............", e.getMessage());
			throw new Exception("download file error............");
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (Exception e) {
					e.printStackTrace();
					throw new Exception("close stream error..........");
				}
			}
		}
	}

	/**
	 * 上传
	 * @param upload_path 上传文件路径	
	 * @param ftp_path	服务器保存路径	
	 * @param newFileName	新文件名
	 * @throws Exception
	 */
	public static void uploadFile(String upload_path, String ftp_path, String newFileName) throws Exception {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(new File(upload_path));
			ftpClient.cd(ftp_path);
			ftpClient.put(fis, newFileName);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Upload file error.");
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
					throw new Exception("close stream error.");
				}
			}
		}
	}

	/**
	 * 关闭
	 *
	 * @throws Exception
	 */
	public static void close() throws Exception {
		loggerMonitor.debug("close............");
		try {
			ftpClient.disconnect();
			sshSession.disconnect();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Exception("close stream error.");
		}
	}

	public static void main(String[] args) {
		try {
			getConnect("ip地址", "22", "用户名", "密码");
			close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

调用方法开启连接通道,完成上传下载文件后,记得调用方法关闭连接。

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值