SFTP client 实例

来源:互联网 发布:centos加固 编辑:程序博客网 时间:2024/05/22 02:01

依赖jar包:jsch-0.1.43.jar

下载地址http://www.jcraft.com/jsch/


[java] view plain copy
  1. package com.sftp;  
  2.   
  3. /* 
  4.  * Created on 2009-9-14 
  5.  * Copyright 2009 by www.xfok.net. All Rights Reserved 
  6.  * 
  7.  */  
  8.   
  9. import java.io.File;  
  10. import java.io.FileInputStream;  
  11. import java.io.FileOutputStream;  
  12. import java.util.Properties;  
  13. import java.util.Vector;  
  14.   
  15. import com.jcraft.jsch.Channel;  
  16. import com.jcraft.jsch.ChannelSftp;  
  17. import com.jcraft.jsch.ChannelSftp.LsEntry;  
  18. import com.jcraft.jsch.JSch;  
  19. import com.jcraft.jsch.Session;  
  20. import com.jcraft.jsch.SftpException;  
  21.   
  22. /** 
  23.  * @author YangHua 
  24.  * 转载请注明出处:http://www.xfok.net/2009/10/124485.html 
  25.  */  
  26. public class SFTPUtils {  
  27.   
  28. //  private static final String host = "192.168.1.168";  
  29. //  private static final  int port = 22;  
  30. //  private static final  String username = "tmux";  
  31. //  private static final  String password = "tmux";  
  32. //  private static final  String directory = "/data/backup/168";  
  33. //    
  34. //  private static final  String downloadFile = "xxx.txt";  
  35. //  private static final  String uploadFile = "d:/cc.jpg";  
  36. //  private static final  String saveFile = "e:/cc.jpg";  
  37. //  private static final  String deleteFile = "cc.jpg";  
  38.   
  39.     /** 
  40.      * 连接sftp服务器 
  41.      * @param host 主机 
  42.      * @param port 端口 
  43.      * @param username 用户名 
  44.      * @param password 密码 
  45.      * @return 
  46.      */  
  47.     public ChannelSftp connect(String host, int port, String username,  
  48.             String password) {  
  49.         ChannelSftp sftp = null;  
  50.         try {  
  51.             JSch jsch = new JSch();  
  52.             jsch.getSession(username, host, port);  
  53.             Session sshSession = jsch.getSession(username, host, port);  
  54.             System.out.println("Session created.");  
  55.             sshSession.setPassword(password);  
  56.             Properties sshConfig = new Properties();  
  57.             sshConfig.put("StrictHostKeyChecking""no");  
  58.             sshSession.setConfig(sshConfig);  
  59.             sshSession.connect();  
  60.             System.out.println("Session connected.");  
  61.             System.out.println("Opening Channel.");  
  62.             Channel channel = sshSession.openChannel("sftp");  
  63.             channel.connect();  
  64.             sftp = (ChannelSftp) channel;  
  65.             System.out.println("Connected to " + host + ".");  
  66.         } catch (Exception e) {  
  67.             e.printStackTrace() ;  
  68.         }  
  69.         return sftp;  
  70.     }  
  71.   
  72.     /** 
  73.      * 上传文件 
  74.      * @param directory 上传的目录 
  75.      * @param uploadFile 要上传的文件 
  76.      * @param sftp 
  77.      */  
  78.     public void upload(String directory, String uploadFile, ChannelSftp sftp) {  
  79.         try {  
  80.             sftp.cd(directory);  
  81.             File file=new File(uploadFile);  
  82.             sftp.put(new FileInputStream(file), file.getName());  
  83.         } catch (Exception e) {  
  84.             e.printStackTrace();  
  85.         }  
  86.     }  
  87.   
  88.     /** 
  89.      * 下载文件 
  90.      * @param directory 下载目录 
  91.      * @param downloadFile 下载的文件 
  92.      * @param saveFile 存在本地的路径 
  93.      * @param sftp 
  94.      */  
  95.     public void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp) {  
  96.         try {  
  97.             sftp.cd(directory);  
  98.             File file=new File(saveFile);  
  99.             sftp.get(downloadFile, new FileOutputStream(file));  
  100.         } catch (Exception e) {  
  101.             e.printStackTrace();  
  102.         }  
  103.     }  
  104.   
  105.     /** 
  106.      * 删除文件 
  107.      * @param directory 要删除文件所在目录 
  108.      * @param deleteFile 要删除的文件 
  109.      * @param sftp 
  110.      */  
  111.     public void delete(String directory, String deleteFile, ChannelSftp sftp) {  
  112.         try {  
  113.             sftp.cd(directory);  
  114.             sftp.rm(deleteFile);  
  115.         } catch (Exception e) {  
  116.             e.printStackTrace();  
  117.         }  
  118.     }  
  119.   
  120.     /** 
  121.      * 列出目录下的文件 
  122.      * @param directory 要列出的目录 
  123.      * @param sftp 
  124.      * @return 
  125.      * @throws SftpException 
  126.      */    
  127.     @SuppressWarnings("unchecked")  
  128.     public Vector<LsEntry> listFiles(String directory, ChannelSftp sftp) throws SftpException{  
  129.         return sftp.ls(directory);  
  130.     }  
  131.   
  132.     public static void main(String[] args) throws Exception {  
  133. //      MySFTP sf = new MySFTP();  
  134. //      ChannelSftp  sftp = sf.connect(host, port, username, password); //获取连接  
  135. //  
  136. //      //sf.upload(directory, uploadFile, sftp);   //上传文件  
  137. //  
  138. //      sf.download(directory, downloadFile, saveFile, sftp);  //删除文件  
  139.   
  140.         //  sf.delete(directory, deleteFile, sftp); //删除文件  
  141.   
  142.         //      Vector<LsEntry> files = sf.listFiles(directory, sftp);        //查看文件列表  
  143.         //      for (LsEntry file : files) {    
  144.         //          System.out.println(file.getFilename());  
  145.         //      }  
  146.   
  147.     }  
  148. }  
0 0