一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能

Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能

2021-05-05 11:44StaticKing Java教程

這篇文章主要介紹了Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能,本文是小編整理的代碼筆記,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

 最近再開發(fā)中遇到需要將文件上傳到linux服務(wù)器上,至此整理代碼筆記。

此種連接方法中有考慮到并發(fā)問題,在進(jìn)行創(chuàng)建ftp連接的時候?qū)⒚恳粋€連接對象存放至

threadlocal<ftp> 中以確保每個線程之間對ftp的打開與關(guān)閉互不影響。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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);
    //根據(jù)用戶名,密碼,端口號獲取session
    sshsession = jsch.getsession(username, host, port);
    sshsession.setpassword(password);
    //修改服務(wù)器/etc/ssh/sshd_config 中 gssapiauthentication的值yes為no,解決用戶不能遠(yuǎn)程登錄
    sshsession.setconfig("userauth.gssapi-with-mic", "no");
    //為session對象設(shè)置properties,第一次訪問服務(wù)器時不用輸入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()) {
      //將新連接防止本地線程,實現(xiàn)并發(fā)處理
      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("關(guān)閉連接" + sftplocal.get().sshsession);
      sftplocal.set(null);
    }
  }
  /**
   * 關(guān)閉通道
   *
   * @throws exception
   */
  public void closechannel() {
    if (null != channel) {
      try {
        channel.disconnect();
      } catch (exception e) {
        logger.error("關(guān)閉sftp通道發(fā)生異常:", e);
      }
    }
    if (null != sshsession) {
      try {
        sshsession.disconnect();
      } catch (exception e) {
        logger.error("sftp關(guān)閉 session異常:", e);
      }
    }
  }
  /**
   * @param directory 上傳ftp的目錄
   * @param uploadfile 本地文件目錄
   *
   */
  public void upload(string directory, string uploadfile) throws exception {
    try {<br>    //執(zhí)行列表展示ls 命令
    channel.ls(directory);<br>    //執(zhí)行盤符切換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() + "關(guān)閉文件時.....異常!" + e.getmessage());
      }
      if (file.exists()) {
        boolean b = file.delete();
        logger.info(file.getname() + "文件上傳完畢!刪除標(biāo)識:" + b);
      }
    }
    }catch (exception e) {
      logger.error("【子目錄創(chuàng)建中】:",e);
            //創(chuàng)建子目錄
      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;
  }
}

總結(jié)

以上所述是小編給大家介紹的java遠(yuǎn)程連接linux服務(wù)器并執(zhí)行命令及上傳文件,希望對大家有所幫助如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

原文鏈接:https://www.cnblogs.com/staticking/p/9082648.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99热都是精品| 欧美日韩视频在线成人 | 欧美性黑人巨大gaysex | 高清女主播一区二区三区 | 好大用力深一点视频 | 狠狠躁夜夜躁人人爽天天miya | 国产日产精品久久久久快鸭 | 日本wwxx| 满溢游泳池免费土豪全集下拉版 | 2021国产麻豆剧传媒剧情 | 亚洲欧美综合在线观看 | 日韩一区二区三 | 高清在线一区二区 | 四虎影院免费在线 | 欧美视频一区二区专区 | 摸逼网| 女仆掀起蕾丝裙被打屁股作文 | 国产精品51麻豆cm传媒 | 成人区精品一区二区毛片不卡 | 久久热这里面只有精品 | 青柠影院在线观看免费完整版1 | 国产精品日本一区二区不卡视频 | 嫩模被黑人粗大挺进 | 国产精品日本亚洲777 | 成人au免费视频影院 | 深夜福利免费观看 | 五月天淫 | aⅴ导航站 | 99在线视频精品 | 日本理论片中文在线观看2828 | 国产精品亚洲精品日韩已方 | 免费在线观看亚洲 | 国产精品一区二区三区免费 | 国产高清在线视频一区二区三区 | 国产精品成人va在线观看 | 欧美z0z0人禽交 | 成人久久18免费网站 | 4438全国最大免费观看 | 欧美在线一| 亚洲国产精品综合欧美 | 国产色网 |