package com.qdb.paygw.util.sftp; import com.jcraft.jsch.*; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Properties; /** * Created by yanghonggang on 2017/6/26. * 用jsch实现sftp文件的上传下载 * * 1:没有考虑校验文件的完整性,文件完整性无法根据文件大小校验(不同操作系统,文件块大小不一样) * 完整性最好由上游系统通过传送协议保证 * * 2:没有实现文件上传下载重试的功能,上游系统可以根据捕获的异常或者文件大小的差异去重试 * */ public final class SftpUtil { public static void main(String[] args) { try { Session session = getSession("103.37.152.107", "backsrvftp_up", "", 8000); ChannelSftp channelSftp = getChannelSftp(session); createDir("/home/backsrvftp_up/test/xx/tt", channelSftp); upload("/Users/yanghonggang/《Java性能优化权威指南》.pdf", "/home/backsrvftp_up/test/xx/tt/《Java性能优化权威指南》.pdf", channelSftp); } catch (Exception e) { e.printStackTrace(); } } /** * 以覆盖的方式上传文件 * @param src 本地文件绝对路径 * @param dst 目标文件绝对路径 * @param sftp 与远程服务器的sftp连接 * @throws SftpException * @throws FileNotFoundException */ public static void upload(String src, String dst, ChannelSftp sftp) throws SftpException, FileNotFoundException { sftp.put(new FileInputStream(src), dst, new ProgressMonitor(), ChannelSftp.OVERWRITE); } /** * 创建远程服务器目录,支持多级 * @param createpath * @param sftp * @throws SftpException */ public static void createDir(String createpath, ChannelSftp sftp) throws SftpException { if (isDirExist(createpath, sftp)) { sftp.cd(createpath); } String pathArr[] = createpath.split("/"); StringBuffer filePath = new StringBuffer("/"); for (String path : pathArr) { if (path.equals("")) { continue; } filePath.append(path + "/"); if (isDirExist(filePath.toString(), sftp)) { sftp.cd(filePath.toString()); } else { // 建立目录 sftp.mkdir(filePath.toString()); // 进入并设置为当前目录 sftp.cd(filePath.toString()); } } sftp.cd(createpath); } /** *判断目录是否存在 * @param directory * @param sftp * @return boolean */ public static boolean isDirExist(String directory, ChannelSftp sftp) { boolean isDirExistFlag = false; try { SftpATTRS sftpATTRS = sftp.lstat(directory); isDirExistFlag = true; return sftpATTRS.isDir(); } catch (SftpException e) { if (e.getMessage().toLowerCase().equals("no such file")) { isDirExistFlag = false; } } return isDirExistFlag; } /** * 获得sftp通道 * * @param session 远程连接的session * @return ChannelSftp * @throws JSchException */ public static ChannelSftp getChannelSftp(Session session) throws JSchException { Channel channel = (Channel) session.openChannel("sftp"); channel.connect(1000); return (ChannelSftp) channel; } /** * 获得远程服务器的session * * @param host 主机ip活主机名 * @param user 用户名 * @param passwd 密码 * @param port 端口,小于等于0就默认22 * @return ssh session * @throws JSchException */ public static Session getSession(String host, String user, String passwd, Integer port) throws JSchException { Session session = (port <= 0) ? new JSch().getSession(user, host) : new JSch().getSession(user, host, port); if (session == null) { throw new JSchException("session is null"); } session.setPassword(passwd); //设置第一次登陆的时候提示,可选值:(ask | yes | no) session.setConfig(new Properties() {{ put("StrictHostKeyChecking", "no"); }}); session.connect(10*1000); return session; } /** * 上传下载进度条 */ public static class ProgressMonitor implements SftpProgressMonitor { private long transfered=-1; @Override public boolean count(long count) { transfered = transfered + count; System.out.println("Currently transferred total size: " + transfered + " Bytes"); return true; } /** * 当传输结束时,调用end方法 */ public void end() { System.out.println("Transferring done."); } /** * 当文件开始传输时,调用init方法 */ public void init(int op, String src, String dest, long max) { System.out.println("Transferring begin."); } } }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31470622/viewspace-2141313/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31470622/viewspace-2141313/