需要用到的JAR包
<dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>262</version> </dependency>
工具類
public class ScpClient { private static ScpClient instance; private String ip="114.116.23.456"; private int port=22; private String name="root"; private String password="123456"; /** * 私有化默認構造函數 實例化對象只能通過getInstance */ private ScpClient() { } /** * 私有化有參構造函數 * * @param ip 服務器ip * @param port 服務器端口 22 * @param name 登錄名 * @param password 登錄密碼 */ private ScpClient(String ip, int port, String name, String password) { this.ip = ip; this.port = port; this.name = name; this.password = password; } /** * download * * @param remoteFile 服務器上的文件名 * @param remoteTargetDirectory 服務器上文件的所在路徑 * @param newPath 下載文件的路徑 */ public void downloadFile(String remoteFile, String remoteTargetDirectory, String newPath) { Connection connection = new Connection(ip, port); try { connection.connect(); boolean isAuthenticated = connection.authenticateWithPassword(name, password); if (isAuthenticated) { SCPClient scpClient = connection.createSCPClient(); SCPInputStream sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile); File f = new File(newPath); if (!f.exists()) { f.mkdirs(); } File newFile = new File(newPath + remoteFile); FileOutputStream fos = new FileOutputStream(newFile); byte[] b = new byte[4096]; int i; while ((i = sis.read(b)) != -1) { fos.write(b, 0, i); } fos.flush(); fos.close(); sis.close(); connection.close(); System.out.println("download ok"); } else { System.out.println("連接建立失敗"); } } catch (IOException e) { e.printStackTrace(); } } /** * download * * @param remoteFile 服務器上的文件名 * @param remoteTargetDirectory 服務器上文件的所在路徑*/ public void download(String remoteFile, String remoteTargetDirectory,HttpServletResponse response) { Connection connection = new Connection(ip, port); try { //1.設置文件ContentType類型,這樣設置,會自動判斷下載文件類型 response.setContentType("multipart/form-data"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(remoteFile, "UTF-8"));// connection.connect(); boolean isAuthenticated = connection.authenticateWithPassword(name, password); if (isAuthenticated) { SCPClient scpClient = connection.createSCPClient(); SCPInputStream sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile); OutputStream outs=response.getOutputStream(); byte[] b = new byte[4096]; int i; while ((i = sis.read(b)) != -1) { outs.write(b, 0, i); } outs.flush(); outs.close(); sis.close(); connection.close(); System.out.println("download ok"); } else { System.out.println("連接建立失敗"); } } catch (IOException e) { e.printStackTrace(); }finally { if(instance!=null) { instance=null; } } } /** * 獲取服務器上相應文件的流 * * @param remoteFile 文件名 * @param remoteTargetDirectory 文件路徑 * @return * @throws IOException */ public SCPInputStream getStream(String remoteFile, String remoteTargetDirectory) throws IOException { Connection connection = new Connection(ip, port); connection.connect(); boolean isAuthenticated = connection.authenticateWithPassword(name, password); if (!isAuthenticated) { System.out.println("連接建立失敗"); return null; } SCPClient scpClient = connection.createSCPClient(); return scpClient.get(remoteTargetDirectory + "/" + remoteFile); } /** * 上傳文件到服務器 * * @param f 文件對象 * @param length 文件大小 * @param remoteTargetDirectory 上傳路徑 * @param mode 默認為null */ public void uploadFile(File f, String remoteTargetDirectory, String mode) { Connection connection = new Connection(ip, port); try { connection.connect(); boolean isAuthenticated = connection.authenticateWithPassword(name, password); if (!isAuthenticated) { System.out.println("連接建立失敗"); return; } SCPClient scpClient = new SCPClient(connection); SCPOutputStream os = scpClient.put(f.getName(), f.length(), remoteTargetDirectory, mode); byte[] b = new byte[4096]; FileInputStream fis = new FileInputStream(f); int i; while ((i = fis.read(b)) != -1) { os.write(b, 0, i); } os.flush(); fis.close(); os.close(); connection.close(); System.out.println("upload ok"); } catch (IOException e) { e.printStackTrace(); } } /** * 單例模式 懶漢式 線程安全 * * @return */ public static ScpClient getInstance() { if (null == instance) { synchronized (ScpClient.class) { if (null == instance) { instance = new ScpClient(); } } } return instance; } public static ScpClient getInstance(String ip, int port, String name, String password) { if (null == instance) { synchronized (ScpClient.class) { if (null == instance) { instance = new ScpClient(ip, port, name, password); } } } return instance; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public static void main(String[] args) { File f1 = new File("C:/Users/Administrator/Desktop/1577926953aaaa.png"); System.out.println(IPUtil.getServerIpAndPort()); ScpClient.getInstance("114.116.124.124", 22, "root", "123456").uploadFile(f1, "/usr/local", null); ScpClient.getInstance().downloadFile("aaa.pang","/usr/local","E:/"); } }
