使用Java怎么远程连接Linux服务器

本篇文章为大家展示了使用Java怎么远程连接Linux服务器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联公司专注于额敏企业网站建设,成都响应式网站建设公司,商城网站制作。额敏网站建设公司,为额敏等地区提供建站服务。全流程按需开发,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务

ThreadLocal 中以确保每个线程之间对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 sftpLocal = new ThreadLocal();
  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 {
    //执行列表展示ls 命令     channel.ls(directory);
    //执行盘符切换cd 命令     channel.cd(directory);     List files = getFiles(uploadFile, new ArrayList());     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 getFiles(String realpath, List 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的特点有哪些

Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

上述内容就是使用Java怎么远程连接Linux服务器,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


网页名称:使用Java怎么远程连接Linux服务器
URL标题:http://myzitong.com/article/psejcg.html