Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能
最近再開發(fā)中遇到需要將文件上傳到Linux服務(wù)器上,至此整理代碼筆記。
此種連接方法中有考慮到并發(fā)問(wèn)題,在進(jìn)行創(chuàng)建FTP連接的時(shí)候?qū)⒚恳粋€(gè)連接對(duì)象存放至
ThreadLocal<Ftp> 中以確保每個(gè)線程之間對(duì)FTP的打開與關(guān)閉互不影響。
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ù)用戶名,密碼,端口號(hào)獲取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對(duì)象設(shè)置properties,第一次訪問(wèn)服務(wù)器時(shí)不用輸入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(); } /** * 獲取本地線程存儲(chǔ)的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()) { //將新連接防止本地線程,實(shí)現(xiàn)并發(fā)處理 sftpLocal.set(new Ftp(host, port, username, password)); } return sftpLocal.get(); } /** * 釋放本地線程存儲(chǔ)的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)閉文件時(shí).....異常!" + e.getMessage()); } if (file.exists()) { boolean b = file.delete(); logger.info(file.getName() + "文件上傳完畢!刪除標(biāo)識(shí):" + 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í)行命令及上傳文件,希望對(duì)大家有所幫助如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Spring Boot中使用LDAP來(lái)統(tǒng)一管理用戶信息的示例
本篇文章主要介紹了Spring Boot中使用LDAP來(lái)統(tǒng)一管理用戶信息的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01使用socket進(jìn)行服務(wù)端與客戶端傳文件的方法
這篇文章主要介紹了使用socket進(jìn)行服務(wù)端與客戶端傳文件的方法,需要的朋友可以參考下2017-08-08Mybatis如何獲取insert新增數(shù)據(jù)id值
這篇文章主要介紹了Mybatis如何獲取insert新增數(shù)據(jù)id值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Java面試崗常見問(wèn)題之ArrayList和LinkedList的區(qū)別
ArrayList和LinkedList作為我們Java中最常使用的集合類,很多人在被問(wèn)到他們的區(qū)別時(shí),憋了半天僅僅冒出一句:一個(gè)是數(shù)組一個(gè)是鏈表。這樣回答簡(jiǎn)直讓面試官吐血。為了讓兄弟們打好基礎(chǔ),我們通過(guò)實(shí)際的使用測(cè)試,好好說(shuō)一下ArrayList和LinkedList的區(qū)別這道經(jīng)典的面試題2022-01-01利用?SpringBoot?在?ES?中實(shí)現(xiàn)類似連表查詢功能
這篇文章主要介紹了如何利用?SpringBoot?在?ES?中實(shí)現(xiàn)類似連表的查詢功能,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07解決SpringBoot配置文件項(xiàng)目重啟出現(xiàn)亂碼的問(wèn)題
最近在創(chuàng)建了SpringBoot項(xiàng)目后往配置文件中寫了相關(guān)的系統(tǒng)配置,并且在上面加了中文注釋,但是在重啟項(xiàng)目或開機(jī)重啟后遇到了注釋亂碼的情況,下面這篇文章主要給大家介紹一下如何解決SpringBoot配置文件項(xiàng)目重啟出現(xiàn)亂碼的問(wèn)題,需要的朋友可以參考下2023-06-06Springboot整合多數(shù)據(jù)源代碼示例詳解
這篇文章主要介紹了Springboot整合多數(shù)據(jù)源代碼示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Java運(yùn)行時(shí)jar終端輸出的中文日志亂碼兩種解決方式
jar包啟動(dòng),今天java開發(fā)過(guò)來(lái)找,說(shuō)jar包啟動(dòng)日志是亂碼,這篇文章主要給大家介紹了關(guān)于Java運(yùn)行時(shí)jar終端輸出的中文日志亂碼的兩種解決方式,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01