jsch 实现 文件上传

 

maven导入

 

com.jcraft

jsch

0.1.53

工具类

​
package com.yj.tr.ch.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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;
import com.yj.tr.ch.context.ChSContext;
import com.yj.tr.resource.update.ResourceUpdateContext;

/***
 * 使用jsch进行文件上传 
* ClassName: RemoteShellUtils * * @author XuL * @date 2018年9月29日 */ public class RemoteShellUtils { private static final Logger LOGGER = LoggerFactory.getLogger(RemoteShellUtils.class); private static Session session = null; private static Channel channel = null; private static ChannelSftp sftp = null; /*** * 创建session 得到session连接 * * @return */ private static Session getSession() throws Exception { LOGGER.info("start create session "); JSch jsch = new JSch(); session = jsch.getSession(ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_USER), ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_HOST), Integer.parseInt(ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_PORT))); session.setPassword(ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_PASSWORD)); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); // 不验证 HostKey session.setConfig(config); try { session.connect(); } catch (Exception e) { if (session.isConnected()) { session.disconnect(); } LOGGER.error( "连接服务器失败,请检查主机[" + ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_HOST) + "],端口[" + ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_PORT) + "],用户名[" + ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_USER) + "]]是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.", e); return null; } return session; } /** * 创建ChannelExec * * @return * @throws JSchException */ private static Channel getChannel() throws Exception { LOGGER.info("start create channel "); if (session == null) { session = getSession(); } if (channel == null) { channel = session.openChannel("sftp"); } try { channel.connect(); } catch (Exception e) { if (channel.isConnected()) channel.disconnect(); LOGGER.error( "连接服务器失败,请检查主机[" + ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_HOST) + "],端口[" + ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_PORT) + "],用户名[" + ChSContext.getContext().getString(ResourceUpdateContext.RESOURCE1_SERVER_USER) + "]]是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.", e); return null; } if (sftp == null) { sftp = (ChannelSftp) channel; } return channel; } /** * 断开连接 * */ private static void disConn() throws Exception { if (null != sftp) { LOGGER.info("disable sftp "); sftp.disconnect(); sftp.exit(); sftp = null; } if (null != channel) { LOGGER.info("disable channel "); channel.disconnect(); channel = null; } if (null != session) { LOGGER.info("disable session "); session.disconnect(); session = null; } } /** * 上传文件 * * @param directory 上传的目录-相对于SFPT设置的用户访问目录, 为空则在SFTP设置的根目录进行创建文件(除设置了服务器全磁盘访问) * @param uploadFile 要上传的文件全路径 */ public static boolean upload(String directory, String uploadFile) throws Exception { channel = getChannel(); if (channel == null || session == null || sftp == null) { return false; } try { try { sftp.cd(directory); // 进入目录 } catch (SftpException sException) { if (sftp.SSH_FX_NO_SUCH_FILE == sException.id) { // 指定上传路径不存在 sftp.mkdir(directory);// 创建目录 sftp.cd(directory); // 进入目录 } } File file = new File(uploadFile); InputStream in = new FileInputStream(file); sftp.put(in, file.getName()); in.close(); } catch (Exception e) { throw new Exception(e.getMessage(), e); } finally { disConn(); } return true; } /*** * 检查文件是否存在 * * @param directory 文件在服务器的路径 * @param fileName 文件名称 */ public static boolean checkFile(String directory, String fileName) throws Exception { LOGGER.info("check is have File {} {}", directory, fileName); boolean isHave = false; try { if (session == null) { channel = getChannel(); } // 得到所有文件名 try { sftp.cd(directory); // 进入目录 } catch (SftpException sException) { if (sftp.SSH_FX_NO_SUCH_FILE == sException.id) { // 指定上传路径不存在 sftp.mkdir(directory);// 创建目录 sftp.cd(directory); // 进入目录 } } // 得到所有文件 Vector filesName = sftp.ls(directory); Iterator it = filesName.iterator(); while (it.hasNext()) { String nameString = ((ChannelSftp.LsEntry) it.next()).getFilename(); if (".".equals(nameString) || "..".equals(nameString)) { continue; } if (fileName.equals(nameString)) { isHave = true; } } } catch (Exception e) { throw new Exception(e.getMessage(), e); } finally { disConn(); } return isHave; } } ​

 

你可能感兴趣的:(jsch)