Windows系統(tǒng)下如何使用Java代碼操作Linux?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純热荩屛覀円黄饋砜纯窗桑?/p>
創(chuàng)新互聯公司是一家集網站建設,薌城企業(yè)網站建設,薌城品牌網站建設,網站定制,薌城網站建設報價,網絡營銷,網絡優(yōu)化,薌城網站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網站。
一、場景描述:
主項目(Web)部署在Windows下,算法項目(TensorFlow)部署在Linux環(huán)境下。
二、依賴環(huán)境(Jar)
<!--Java SSH插件--> <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh3</artifactId> <version>build210</version> </dependency> <dependency> <groupId>sshtools</groupId> <artifactId>j2ssh-core</artifactId> <version>0.2.9</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
三、后端代碼
package cn.virgo.audio.utils; import ch.ethz.ssh3.ChannelCondition; import ch.ethz.ssh3.Connection; import ch.ethz.ssh3.Session; import ch.ethz.ssh3.StreamGobbler; import com.sshtools.j2ssh.SshClient; import com.sshtools.j2ssh.authentication.AuthenticationProtocolState; import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient; import com.sshtools.j2ssh.sftp.SftpFile; import org.apache.commons.io.IOUtils; import java.io.*; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; public class RemoteShellExecutor { private Connection conn; private String ip; private String userName; private String password; private String charset = Charset.defaultCharset().toString(); private static final int TIME_OUT = 1000 * 5 * 60; /** * 構造函數 * * @param ip * @param userName * @param password */ public RemoteShellExecutor(String ip, String userName, String password) { this.ip = ip; this.userName = userName; this.password = password; } /** * 鏈接遠程桌面 * * @return * @throws IOException */ private boolean login() throws IOException { conn = new ch.ethz.ssh3.Connection(ip); conn.connect(); return conn.authenticateWithPassword(userName, password); } /** * 執(zhí)行shell * * @param cmds * @return * @throws Exception */ public int exec(String cmds) throws Exception { InputStream stdOut = null; InputStream stdErr = null; int ret = -1; try { if (login()) { Session session = conn.openSession(); session.execCommand(cmds); stdOut = new StreamGobbler(session.getStdout()); processStream(stdOut, charset); stdErr = new StreamGobbler(session.getStderr()); processStream(stdErr, charset); session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT); ret = session.getExitStatus(); } else { throw new Exception("遠程鏈接失敗:" + ip); } } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { conn.close(); } IOUtils.closeQuietly(stdOut); IOUtils.closeQuietly(stdErr); } return ret; } /** * 獲取執(zhí)行過程輸出 * * @param in * @param charset * @return * @throws IOException */ private void processStream(InputStream in, String charset) throws IOException { byte[] buf = new byte[1024]; while (in.read(buf) != -1) { System.out.println(new String(buf, charset)); } } /** * 獲取Linux下某個文件數據,將其拷貝到本地tmpPath下 */ public List<String> getCaleResByFileFromSSH(String filePath, String filename, String tmpPath) { List<String> resList = new ArrayList<>(); SshClient client = new SshClient(); try { client.connect(this.ip); //設置用戶名和密碼 PasswordAuthenticationClient pwd = new PasswordAuthenticationClient(); pwd.setUsername(this.userName); pwd.setPassword(this.password); int result = client.authenticate(pwd); if (result == AuthenticationProtocolState.COMPLETE) {//如果連接完成 List<SftpFile> list = client.openSftpClient().ls(filePath); for (SftpFile f : list) { if (f.getFilename().equals(filename)) { OutputStream os = new FileOutputStream(tmpPath + f.getFilename()); client.openSftpClient().get(f.getAbsolutePath(), os); //以行為單位讀取文件start File file = new File(tmpPath + f.getFilename()); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1;//行號 //一次讀入一行,直到讀入null為文件結束 while ((tempString = reader.readLine()) != null) { //顯示行號 System.out.println("line " + line + ": " + tempString); resList.add(tempString); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } //以行為單位讀取文件end } } } } catch (IOException e) { e.printStackTrace(); } return resList; } }
感謝各位的閱讀!看完上述內容,你們對Windows系統(tǒng)下如何使用Java代碼操作Linux大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道。
本文題目:Windows系統(tǒng)下如何使用Java代碼操作Linux
本文鏈接:http://www.ekvhdxd.cn/article4/gecpoe.html
成都網站建設公司_創(chuàng)新互聯,為您提供標簽優(yōu)化、網頁設計公司、網站導航、App開發(fā)、企業(yè)網站制作、網站制作
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯