利用ssh實(shí)現(xiàn)服務(wù)器文件上傳下載
通過(guò)ssh實(shí)現(xiàn)服務(wù)器文件上傳下載
寫(xiě)在前面的話
之前記錄過(guò)一篇使用apache的FTP開(kāi)源組件實(shí)現(xiàn)服務(wù)器文件上傳下載的方法,但是后來(lái)發(fā)現(xiàn)在刪除的時(shí)候會(huì)有些權(quán)限問(wèn)題,導(dǎo)致無(wú)法刪除服務(wù)器上的文件。雖然在Windows上使用FileZilla Server設(shè)置讀寫(xiě)權(quán)限后沒(méi)問(wèn)題,但是在服務(wù)器端還是有些不好用。
因?yàn)樽约盒枰獙?shí)現(xiàn)資源管理功能,除了單文件的FastDFS存儲(chǔ)之外,一些特定資源的存儲(chǔ)還是打算暫時(shí)存放服務(wù)器上,項(xiàng)目組同事說(shuō)后面不會(huì)專(zhuān)門(mén)在服務(wù)器上開(kāi)FTP服務(wù),于是改成了sftp方式進(jìn)行操作。
這個(gè)東西要怎么用
首先要去下載jsch jar包,地址是:http://www.jcraft.com/jsch/。網(wǎng)站上也寫(xiě)的很清楚:JSch is a pure Java implementation of SSH2. 這個(gè)是SSH2的純Java實(shí)現(xiàn)。使用ip和端口,輸入用戶(hù)名密碼就可以正常使用了,和Secure CRT使用方式一致。那么怎么來(lái)使用這個(gè)有用的工具呢?
其實(shí)不會(huì)寫(xiě)也沒(méi)關(guān)系,官方也給出了示例,鏈接:http://www.jcraft.com/jsch/examples/Shell.java,來(lái)看一眼吧:
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ /** * This program enables you to connect to sshd server and get the shell prompt. * $ CLASSPATH=.:../build javac Shell.java * $ CLASSPATH=.:../build java Shell * You will be asked username, hostname and passwd. * If everything works fine, you will get the shell prompt. Output may * be ugly because of lacks of terminal-emulation, but you can issue commands. * */ import com.jcraft.jsch.*; import java.awt.*; import javax.swing.*; public class Shell{ public static void main(String[] arg){ try{ JSch jsch=new JSch(); //jsch.setKnownHosts("/home/foo/.ssh/known_hosts"); String host=null; if(arg.length>0){ host=arg[0]; } else{ host=JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name")+ "@localhost"); } String user=host.substring(0, host.indexOf('@')); host=host.substring(host.indexOf('@')+1); Session session=jsch.getSession(user, host, 22); String passwd = JOptionPane.showInputDialog("Enter password"); session.setPassword(passwd); UserInfo ui = new MyUserInfo(){ public void showMessage(String message){ JOptionPane.showMessageDialog(null, message); } public boolean promptYesNo(String message){ Object[] options={ "yes", "no" }; int foo=JOptionPane.showOptionDialog(null, message, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); return foo==0; } // If password is not given before the invocation of Session#connect(), // implement also following methods, // * UserInfo#getPassword(), // * UserInfo#promptPassword(String message) and // * UIKeyboardInteractive#promptKeyboardInteractive() }; session.setUserInfo(ui); // It must not be recommended, but if you want to skip host-key check, // invoke following, // session.setConfig("StrictHostKeyChecking", "no"); //session.connect(); session.connect(30000); // making a connection with timeout. Channel channel=session.openChannel("shell"); // Enable agent-forwarding. //((ChannelShell)channel).setAgentForwarding(true); channel.setInputStream(System.in); /* // a hack for MS-DOS prompt on Windows. channel.setInputStream(new FilterInputStream(System.in){ public int read(byte[] b, int off, int len)throws IOException{ return in.read(b, off, (len>1024?1024:len)); } }); */ channel.setOutputStream(System.out); /* // Choose the pty-type "vt102". ((ChannelShell)channel).setPtyType("vt102"); */ /* // Set environment variable "LANG" as "ja_JP.eucJP". ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP"); */ //channel.connect(); channel.connect(3*1000); } catch(Exception e){ System.out.println(e); } } public static abstract class MyUserInfo implements UserInfo, UIKeyboardInteractive{ public String getPassword(){ return null; } public boolean promptYesNo(String str){ return false; } public String getPassphrase(){ return null; } public boolean promptPassphrase(String message){ return false; } public boolean promptPassword(String message){ return false; } public void showMessage(String message){ } public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo){ return null; } } }
在這個(gè)代碼中,我們基本上能看到需要的東西,首先我們要?jiǎng)?chuàng)建用戶(hù)信息,這個(gè)主要是給認(rèn)證的時(shí)候用的,只要實(shí)現(xiàn) UserInfo, UIKeyboardInteractive這兩個(gè)接口就好了,然后通過(guò)創(chuàng)建session會(huì)話,將userInfo設(shè)置進(jìn)去,最后進(jìn)行連接即可。
封裝文件上傳下載
上面是Jsch的基本使用方法,也就是些基本套路。下面我們來(lái)自己封裝一下自己要使用的功能,實(shí)現(xiàn)文件的上傳下載等一系列操作。
首先,也來(lái)創(chuàng)建個(gè)UserInfo:
public class MyUserInfo implements UserInfo, UIKeyboardInteractive{ public String getPassword(){ return null; } public boolean promptYesNo(String str){ return true; } public String getPassphrase(){ return null; } public boolean promptPassphrase(String message){ return true; } public boolean promptPassword(String message){ return true; } public void showMessage(String message){ } @Override public String[] promptKeyboardInteractive(String arg0, String arg1, String arg2, String[] arg3, boolean[] arg4) { return null; } }
下面是實(shí)現(xiàn)類(lèi):
package com.tfxiaozi.common.utils; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Vector; import org.apache.log4j.Logger; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; import com.jcraft.jsch.SftpProgressMonitor; /** * SSH Utils * @author tfxiaozi * */ public class Ssh { Logger logger = Logger.getLogger(this.getClass()); private String host = ""; private String user = ""; private int port = 22; private String password = ""; private static final String PROTOCOL = "sftp"; JSch jsch = new JSch(); private Session session; private Channel channel; private ChannelSftp sftp; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public Ssh() { } public Ssh(String host, int port, String user, String password) { this.host = host; this.user = user; this.password = password; this.port = port; } /** * connect ssh * @throws JSchException */ public void connect() throws JSchException { if (session == null) { session = jsch.getSession(user, host, port); MyUserInfo ui = new MyUserInfo(); session.setUserInfo(ui); session.setPassword(password); session.connect(); channel = session.openChannel(PROTOCOL); channel.connect(); sftp = (ChannelSftp)channel; } } /** * disconnect ssh */ public void disconnect() { if (session != null) { session.disconnect(); session = null; } } /** * upload * @param localFileName * @param remoteFileName * @return */ public boolean upload(String localFileName, String remoteFileName) throws Exception{ boolean bSucc = false; try { SftpProgressMonitor monitor=new MyProgressMonitor(); int mode=ChannelSftp.OVERWRITE; sftp.put(localFileName, remoteFileName, monitor, mode); bSucc = true; } catch(Exception e) { logger.error(e); } finally { if (null != channel) { channel.disconnect(); } } return bSucc; } /** * delete file * @param directory * @param fileName * @return */ public boolean deteleFile(String directory, String fileName) { boolean flag = false; try { sftp.cd(directory); sftp.rm(fileName); flag = true; } catch (SftpException e) { flag = false; logger.error(e); } return flag; } /** * delete directory * @param directory dir to be delete * @param sure be sure to delete * @return */ public String deleteDir(String directory, boolean sure) { String command = "rm -rf " + directory; String result = execCommand(command, true); return result; } /** * compress the files and sub-dir of directory into a zip named compressName * @param directory the content directory to be compress * @param compressName the name in directory after it is compressed * @throws SftpException * @usage ssh.compressDir("/home/tfxiaozi/webapp", "test.zip"); */ public void compressDir(String directory, String compressName) throws SftpException { String command = "cd "+ directory +"\nzip -r " + compressName + " ./" + compressName.substring(0, compressName.lastIndexOf(".")); execCommand(command, true); } /** * download * @param localFileName * @param remoteFileName * @return */ public boolean download(String localFileName, String remoteFileName) { boolean bSucc = false; Channel channel = null; try { SftpProgressMonitor monitor = new MyProgressMonitor(); sftp.get(remoteFileName, localFileName, monitor, ChannelSftp.OVERWRITE); bSucc = true; } catch(Exception e) { logger.error(e); } finally { if (null != channel) { channel.disconnect(); } } return bSucc; } /** * execute command * @param command * @param flag * @return */ public String execCommand(String command, boolean flag) { Channel channel = null; InputStream in = null; StringBuffer sb = new StringBuffer(""); try { channel = session.openChannel("exec"); System.out.println("command:" + command); ((ChannelExec)channel).setCommand("export TERM=ansi && " + command); ((ChannelExec)channel).setErrStream(System.err); in = channel.getInputStream(); channel.connect(); if (flag) { byte[] tmp = new byte[10240]; while (true) { while (in.available()>0) { int i = in.read(tmp, 0, 10240); if(i < 0) { break; } sb.append(new String(tmp, 0, i)); } if (channel.isClosed()){ break; } } } in.close(); } catch(Exception e){ logger.error(e); } finally { if (channel != null) { channel.disconnect(); } } return sb.toString(); } /** * get cpu info * @return */ public String[] getCpuInfo() { Channel channel = null; InputStream in = null; StringBuffer sb = new StringBuffer(""); try { channel = session.openChannel("exec"); ((ChannelExec)channel).setCommand("export TERM=ansi && top -bn 1");//ansi一定要加 in = channel.getInputStream(); ((ChannelExec)channel).setErrStream(System.err); channel.connect(); byte[] tmp = new byte[10240]; while (true) { while (in.available()>0) { int i = in.read(tmp, 0, 10240); if(i < 0) { break; } sb.append(new String(tmp, 0, i)); } if (channel.isClosed()){ break; } } } catch(Exception e){ logger.error(e); } finally { if (channel != null) { channel.disconnect(); } } String buf = sb.toString(); if (buf.indexOf("Swap") != -1) { buf = buf.substring(0, buf.indexOf("Swap")); } if (buf.indexOf("Cpu") != -1) { buf = buf.substring(buf.indexOf("Cpu"), buf.length()); } buf.replaceAll(" ", " "); return buf.split("\\n"); } /** * get hard disk info * @return */ public String getHardDiskInfo() throws Exception{ Channel channel = null; InputStream in = null; StringBuffer sb = new StringBuffer(""); try { channel = session.openChannel("exec"); ((ChannelExec)channel).setCommand("df -lh"); in = channel.getInputStream(); ((ChannelExec)channel).setErrStream(System.err); channel.connect(); byte[] tmp = new byte[10240]; while (true) { while (in.available()>0) { int i = in.read(tmp, 0, 10240); if(i < 0) { break; } sb.append(new String(tmp, 0, i)); } if (channel.isClosed()){ break; } } } catch(Exception e){ throw new RuntimeException(e); } finally { if (channel != null) { channel.disconnect(); } } String buf = sb.toString(); String[] info = buf.split("\n"); if(info.length > 2) {//first line: Filesystem Size Used Avail Use% Mounted on String tmp = ""; for(int i=1; i< info.length; i++) { tmp = info[i]; String[] tmpArr = tmp.split("%"); if(tmpArr[1].trim().equals("/")){ boolean flag = true; while(flag) { tmp = tmp.replaceAll(" ", " "); if (tmp.indexOf(" ") == -1){ flag = false; } } String[] result = tmp.split(" "); if(result != null && result.length == 6) { buf = result[1] + " total, " + result[2] + " used, " + result[3] + " free"; break; } else { buf = ""; } } } } else { buf = ""; } return buf; } /** * 返回空閑字節(jié)數(shù) * @return * @throws Exception */ public double getFreeDisk() throws Exception { String hardDiskInfo = getHardDiskInfo(); if(hardDiskInfo == null || hardDiskInfo.equals("")) { logger.error("get free harddisk space failed....."); return -1; } String[] diskInfo = hardDiskInfo.replace(" ", "").split(","); if(diskInfo == null || diskInfo.length == 0) { logger.error("get free disk info failed........."); return -1; } String free = diskInfo[2]; free = free.substring(0, free.indexOf("free")); //System.out.println("free space:" + free); String unit = free.substring(free.length()-1); //System.out.println("unit:" + unit); String freeSpace = free.substring(0, free.length()-1); double freeSpaceL = Double.parseDouble(freeSpace); //System.out.println("free spaceL:" + freeSpaceL); if(unit.equals("K")) { return freeSpaceL*1024; }else if(unit.equals("M")) { return freeSpaceL*1024*1024; } else if(unit.equals("G")) { return freeSpaceL*1024*1024*1024; } else if(unit.equals("T")) { return freeSpaceL*1024*1024*1024*1024; } else if(unit.equals("P")) { return freeSpaceL*1024*1024*1024*1024*1024; } return 0; } /** * 獲取指定目錄下的所有子目錄及文件 * @param directory * @return * @throws Exception */ @SuppressWarnings("rawtypes") public List<String> listFiles(String directory) throws Exception { Vector fileList = null; List<String> fileNameList = new ArrayList<String>(); fileList = sftp.ls(directory); Iterator it = fileList.iterator(); while (it.hasNext()) { String fileName = ((ChannelSftp.LsEntry) it.next()).getFilename(); if (fileName.startsWith(".") || fileName.startsWith("..")) { continue; } fileNameList.add(fileName); } return fileNameList; } public boolean mkdir(String path) { boolean flag = false; try { sftp.mkdir(path); flag = true; } catch (SftpException e) { flag = false; } return flag; } }
測(cè)試一下
public static void main(String[] arg) throws Exception{ Ssh ssh = new Ssh("10.10.10.83", 22, "test", "test"); try { ssh.connect(); } catch (JSchException e) { e.printStackTrace(); } /*String remotePath = "/home/tfxiaozi/" + "webapp/"; try { ssh.listFiles(remotePath); } catch (Exception e) { ssh.mkdir(remotePath); }*/ /*boolean b = ssh.upload("d:/test.zip", "webapp/"); System.out.println(b);*/ //String []buf = ssh.getCpuInfo(); //System.out.println("cpu:" + buf[0]); //System.out.println("memo:" + buf[1]); //System.out.println(ssh.getHardDiskInfo().replace(" ", "")); //System.out.println(ssh.getFreeDisk()); /*List<String> list = ssh.listFiles("webapp/test"); for(String s : list) { System.out.println(s); }*/ /*boolean b = ssh.deteleFile("webapp", "test.zip"); System.out.println(b);*/ /*try { String s = ssh.execCommand("ls -l /home/tfxiaozi/webapp1/test", true); System.out.println(s); } catch (Exception e) { System.out.println(e.getMessage()); }*/ //ssh.sftp.setFilenameEncoding("UTF-8"); /*try { String ss = ssh.execCommand("unzip /home/tfxiaozi/webapp1/test.zip -d /home/tfxiaozi/webapp1/", true); System.out.println(ss); } catch (Exception e) { System.out.println( e.getMessage()); }*/ /*String path = "/home/tfxiaozi/webapp1/test.zip"; try { List<String> list = ssh.listFiles(path); for(String s:list) { System.out.println(s); } System.out.println("ok"); } catch (Exception e) { System.out.println("extract failed...."); }*/ /*String command = "rm -rf /home/tfxiaozi/webapp1/" + "水墨國(guó)學(xué)"; String sss = ssh.execCommand(command, true); System.out.println(sss);*/ /*String findCommand = "find /home/tfxiaozi/webapp1/水墨國(guó)學(xué) -name 'index.html'"; String result = ssh.execCommand(findCommand, true); System.out.println(result);*/ /*String path = ""; ssh.listFiles(remotePath);*/ /* ssh.deleteDir("/home/tfxiaozi/webapp1", true); */ //下面這個(gè)會(huì)解壓到webapp1目錄,webapp1/test/xxx //ssh.execCommand("unzip /home/tfxiaozi/webapp1/test.zip -d /home/tfxiaozi/webapp1", true); //下面這個(gè)會(huì)解壓到/webapp1/test目錄,也是webapp1/test/test/xxx //ssh.execCommand("unzip /home/tfxiaozi/webapp1/test.zip -d /home/tfxiaozi/webapp1", true); //ssh.compressDir("/home/tfxiaozi/webapp1", "test.zip"); //ssh.sftp.cd("/home/tfxiaozi/webapp1"); //ssh.compressDir("/home/tfxiaozi/webapp1", "test.zip"); /*boolean b = ssh.download("d:/temp/test.zip", "webapp/test.zip"); System.out.println(b);*/ //ssh.getHardDiskInfo(); System.out.println(ssh.getFreeDisk()); ssh.disconnect(); }
以上就是直接使用linux方式進(jìn)行操作,不過(guò)需要注意的是,對(duì)于中文文件,在解壓的時(shí)候,傳入的時(shí)候會(huì)有可能亂碼,需要加上參數(shù),如unzip -O cp936 test.zip -d /home/tfxiaozi/test。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Windows 2008 服務(wù)器安全加固幾個(gè)注意事項(xiàng)
- Windows服務(wù)器的基礎(chǔ)安全加固方法(2008、2012)
- Git 教程之服務(wù)器搭建詳解
- 在一臺(tái)服務(wù)器上安裝兩個(gè)或多個(gè)mysql的實(shí)現(xiàn)步驟
- 騰訊云CentOS 6.6快速安裝 Nginx服務(wù)器圖文教程
- Nginx服務(wù)器Nginx.com配置文件詳解
- SQL Server成功與服務(wù)器建立連接但是在登錄過(guò)程中發(fā)生錯(cuò)誤的快速解決方案
- 安卓手機(jī)socket通信(服務(wù)器和客戶(hù)端)
- Linux服務(wù)器下MariaDB 10自動(dòng)化安裝部署
- Ajax 高級(jí)功能之a(chǎn)jax向服務(wù)器發(fā)送數(shù)據(jù)
- dubbo 管理控制臺(tái)安裝和使用詳解
相關(guān)文章
詳解如何配置springboot跳轉(zhuǎn)html頁(yè)面
這篇文章主要介紹了詳解如何配置springboot跳轉(zhuǎn)html頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09LoggingEventAsyncDisruptorAppender類(lèi)執(zhí)行流程源碼解讀
這篇文章主要介紹了LoggingEventAsyncDisruptorAppender類(lèi)執(zhí)行流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12java泛型的局限探究及知識(shí)點(diǎn)總結(jié)
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于java泛型的局限探究及知識(shí)點(diǎn)總結(jié)內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-07-07spring使用OXM進(jìn)行對(duì)象XML映射解析
這篇文章主要介紹了spring使用OXM進(jìn)行對(duì)象XML映射解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12Java?ynchronized重量級(jí)鎖的核心原理詳解
這篇文章主要為大家詳細(xì)介紹了Java?ynchronized重量級(jí)鎖的核心原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03Java多線程(單例模式,阻塞隊(duì)列,定時(shí)器,線程池)詳解
本文是多線程初級(jí)入門(mén),主要介紹了多線程單例模式、阻塞隊(duì)列、定時(shí)器、線程池、多線程面試考點(diǎn),感興趣的小伙伴可以跟隨小編一起了解一下2022-09-09MyBatis批量查詢(xún)、插入、更新、刪除的實(shí)現(xiàn)示例
由于需要處理短時(shí)間內(nèi)大量數(shù)據(jù)入庫(kù)的問(wèn)題,想到了Mybatis的批量操作,本文主要介紹了MyBatis批量查詢(xún)、插入、更新、刪除的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-05-05Spring?WebMVC初始化Controller流程詳解
這篇文章主要介紹了Spring?WebMVC初始化Controller流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02