最近再开发中遇到需要将文件上传到Linux服务器上,至此整理代码笔记。
此种连接方法中有考虑到并发问题,在进行创建FTP连接的时候将每一个连接对象存放至
ThreadLocal<Ftp> 中以确保每个线程之间对FTP的打开与关闭互不影响。
package com.test.utils; import java.io.BufferedInputStream; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class Ftp { //打印log日志 private static final Log logger = LogFactory.getLog(Ftp.class); private static Date last_push_date = null; private Session sshSession; private ChannelSftp channel; private static ThreadLocal<Ftp> sftpLocal = new ThreadLocal<Ftp>(); private Ftp(String host, int port, String username, String password) throws Exception { JSch jsch = new JSch(); jsch.getSession(username, host, port); //根据用户名,密码,端口号获取session sshSession = jsch.getSession(username, host, port); sshSession.setPassword(password); //修改服务器/etc/ssh/sshd_config 中 GSSAPIAuthentication的值yes为no,解决用户不能远程登录 sshSession.setConfig("userauth.gssapi-with-mic", "no"); //为session对象设置properties,第一次访问服务器时不用输入yes sshSession.setConfig("StrictHostKeyChecking", "no"); sshSession.connect(); //获取sftp通道 channel = (ChannelSftp)sshSession.openChannel("sftp"); channel.connect(); logger.info("连接ftp成功!" + sshSession); } /** * 是否已连接 * * @return */ private boolean isConnected() { return null != channel && channel.isConnected(); } /** * 获取本地线程存储的sftp客户端 * * @return * @throws Exception */ public static Ftp getSftpUtil(String host, int port, String username, String password) throws Exception { //获取本地线程 Ftp sftpUtil = sftpLocal.get(); if (null == sftpUtil || !sftpUtil.isConnected()) { //将新连接防止本地线程,实现并发处理 sftpLocal.set(new Ftp(host, port, username, password)); } return sftpLocal.get(); } /** * 释放本地线程存储的sftp客户端 */ public static void release() { if (null != sftpLocal.get()) { sftpLocal.get().closeChannel(); logger.info("关闭连接" + sftpLocal.get().sshSession); sftpLocal.set(null); } } /** * 关闭通道 * * @throws Exception */ public void closeChannel() { if (null != channel) { try { channel.disconnect(); } catch (Exception e) { logger.error("关闭SFTP通道发生异常:", e); } } if (null != sshSession) { try { sshSession.disconnect(); } catch (Exception e) { logger.error("SFTP关闭 session异常:", e); } } } /** * @param directory 上传ftp的目录 * @param uploadFile 本地文件目录 * */ public void upload(String directory, String uploadFile) throws Exception { try {<br> //执行列表展示ls 命令 channel.ls(directory);<br> //执行盘符切换cd 命令 channel.cd(directory); List<File> files = getFiles(uploadFile, new ArrayList<File>()); for (int i = 0; i < files.size(); i++) { File file = files.get(i); InputStream input = new BufferedInputStream(new FileInputStream(file)); channel.put(input, file.getName()); try { if (input != null) input.close(); } catch (Exception e) { e.printStackTrace(); logger.error(file.getName() + "关闭文件时.....异常!" + e.getMessage()); } if (file.exists()) { boolean b = file.delete(); logger.info(file.getName() + "文件上传完毕!删除标识:" + b); } } }catch (Exception e) { logger.error("【子目录创建中】:",e); //创建子目录 channel.mkdir(directory); } } //获取文件 public List<File> getFiles(String realpath, List<File> files) { File realFile = new File(realpath); if (realFile.isDirectory()) { File[] subfiles = realFile.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (null == last_push_date ) { return true; } else { long modifyDate = file.lastModified(); return modifyDate > last_push_date.getTime(); } } }); for (File file : subfiles) { if (file.isDirectory()) { getFiles(file.getAbsolutePath(), files); } else { files.add(file); } if (null == last_push_date) { last_push_date = new Date(file.lastModified()); } else { long modifyDate = file.lastModified(); if (modifyDate > last_push_date.getTime()) { last_push_date = new Date(modifyDate); } } } } return files; } }
总结
以上所述是小编给大家介绍的Java远程连接Linux服务器并执行命令及上传文件,希望对大家有所帮助如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
问题内容: 如何在远程Linux机器上执行命令/脚本?说我想从框a的框b开始服务tomcat。 问题答案: 我想这是最好的安全方式,例如: 必须根据您的特定需求(例如,仅绑定到ipv4)部署OPTIONS的地方,并且您的远程命令可能正在启动您的tomcat守护程序。 注意 : 如果不想在每次运行ssh时都提示您,也请查看ssh- agent,以及系统允许的情况。关键是…了解ssh密钥交换过程。请仔
问题内容: 我试图通过这样的本地服务器上的ssh命令在远程linux服务器上执行命令: ssh myremoteserver’类型ttisql’ 其中ttisql是我的远程计算机路径上的可执行文件。 运行此命令的结果是: bash:第0行:类型:ttisql:未找到 当我只是先连接并执行以下操作: ssh myremoteserver 然后输入命令: [myuser @ myremoteserve
本文向大家介绍python中使用paramiko模块并实现远程连接服务器执行上传下载功能,包括了python中使用paramiko模块并实现远程连接服务器执行上传下载功能的使用技巧和注意事项,需要的朋友参考一下 paramiko模块 paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加
本文向大家介绍python利用paramiko连接远程服务器执行命令的方法,包括了python利用paramiko连接远程服务器执行命令的方法的使用技巧和注意事项,需要的朋友参考一下 python中的paramiko模块是用来实现ssh连接到远程服务器上的库,在进行连接的时候,可以用来执行命令,也可以用来上传文件。 1、得到一个连接的对象 在进行连接的时候,可以使用如下的代码: 在connect函
本文向大家介绍Python使用paramiko连接远程服务器执行Shell命令的实现,包括了Python使用paramiko连接远程服务器执行Shell命令的实现的使用技巧和注意事项,需要的朋友参考一下 需求 在自动化测试场景里, 有时需要在代码里获取远程服务器的某些数据, 或执行一些查询命令,如获取Linux系统版本号 \ 获取CPU及内存的占用等, 本章记录一下使用paramiko模块SSH连
最近遇到一个这样的需求:传一个压缩包给后台Linux服务器,后台保存后解压读取里面的文件,现学现做。在这里做个记录。 文件上传 文件上传有很多方法,这里推荐一个自己感觉挺好用的一种,代码奉上: