K60至尊版狂暴引擎2.0加持:超177萬跑分斬獲性能第一
Redmi的后性能時代戰略發布會今天下午如期舉辦,在本次發布會上,Redmi公布了多項關于和聯發科的深度合作,以及新機K60 Ultra在軟件和硬件方面的特性,例如:“K60 至尊版,雙芯旗艦
JSch 是一個純 Java 實現的 SSH2 客戶端庫,它允許 Java 應用程序通過 SSH 協議連接到 SSH 服務器,并執行命令、傳輸文件等。JSch 是基于 SSH-2 協議的一個開源項目,廣泛用于需要遠程執行命令或文件傳輸的 Java 應用程序中。
<dependency> <groupId>com.github.mwiede</groupId> <artifactId>jsch</artifactId> <version>0.2.19</version> </dependency>public Session getSession(){ if( this.session != null ){ return this.session; } try { jsch.getSession(property.getUsername(), property.getHost(), property.getPort()); session = jsch.getSession(property.getUsername(), property.getHost(), property.getPort()); session.setPassword(property.getPassword()); session.setConfig("StrictHostKeyChecking","no");// 設置第一次登陸的時候提示 session.setConfig("max_input_buffer_size","1024");// Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); session.setConfig(sshConfig); session.connect(); return session; } catch (JSchException e) { throw new RuntimeException(e); } }public static ChannelSftp getSftp(Session session){ try { Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftp = (ChannelSftp) channel; sftp.setFilenameEncoding("UTF-8"); return sftp; } catch (Exception e) { throw new RuntimeException(e); } }public static ChannelExec getExec(Session session){ try { Channel channel = session.openChannel("exec");// channel.connect(); ChannelExec exec = (ChannelExec) channel; return exec; } catch (Exception e) { throw new RuntimeException(e); } }public static void execCommand(ChannelExec exec,String command){ try { exec.setCommand(command); InputStream in = exec.getInputStream(); exec.connect(); BufferedReader inputReader = new BufferedReader(new InputStreamReader(in, "UTF8")); String inputLine; while ((inputLine = inputReader.readLine()) != null) { System.out.println(inputLine); } } catch (Exception e) { throw new RuntimeException(e); } finally { exec.disconnect(); } }public static void fileDownload(ChannelSftp sftp, String path,String dist){ try { InputStream is = sftp.get(path); FileUtils.copyInputStreamToFile(is, FileUtils.getFile(dist,FilenameUtils.getName(path))); is.close(); } catch (SftpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }public static Session getSession(){ ConnectProperty property = new ConnectProperty(); property.setHost("..."); property.setPort(22); property.setUsername("..."); property.setPassword("..."); ConnectHelper helper = new ConnectHelper(property); return helper.getSession();}public static void download(Session session){ ChannelSftp sftp = ConnectHelper.getSftp(session); ConnectHelper.fileDownload(sftp,"/home/test/1.txt","E://home//tmp");}public static void execCommand(Session session){ ChannelExec exec = ConnectHelper.getExec(session); ConnectHelper.execCommand(exec, "pwd");
本文鏈接:http://m.www897cc.com/showinfo-26-112725-0.html還不會用Java操作遠程服務器?
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com