转:JAVA实现SFTP上传,下载,删除等方法

  1. 原载:http://blog.csdn.net/haidage/article/details/6859716
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.InputStreamReader;  
  9. import java.io.LineNumberReader;  
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12. import java.util.Properties;  
  13.   
  14. import com.jcraft.jsch.Channel;  
  15. import com.jcraft.jsch.ChannelSftp;  
  16. import com.jcraft.jsch.JSch;  
  17. import com.jcraft.jsch.Session;  
  18. import com.jcraft.jsch.SftpException;  
  19.   
  20. public class SFTPUtil {  
  21.       
  22.     private String host = "127.0.0.1";  
  23.     private String username="MingMac";  
  24.     private String password="×××";  
  25.     private int port = 22;  
  26.     private ChannelSftp sftp = null;  
  27.     private String localPath = "/var/test";  
  28.     private String remotePath = "/var/tset";  
  29.     private String fileListPath = "/var/test/java/test.txt";  
  30.     private final String seperator = "/";  
  31.       
  32.     /** 
  33.      * connect server via sftp 
  34.      */  
  35.     public void connect() {  
  36.         try {  
  37.             if(sftp != null){  
  38.                 System.out.println("sftp is not null");  
  39.             }  
  40.             JSch jsch = new JSch();  
  41.             jsch.getSession(username, host, port);  
  42.             Session sshSession = jsch.getSession(username, host, port);  
  43.             System.out.println("Session created.");  
  44.             sshSession.setPassword(password);  
  45.             Properties sshConfig = new Properties();  
  46.             sshConfig.put("StrictHostKeyChecking""no");  
  47.             sshSession.setConfig(sshConfig);  
  48.             sshSession.connect();  
  49.             System.out.println("Session connected.");  
  50.             System.out.println("Opening Channel.");  
  51.             Channel channel = sshSession.openChannel("sftp");  
  52.             channel.connect();  
  53.             sftp = (ChannelSftp) channel;  
  54.             System.out.println("Connected to " + host + ".");  
  55.         } catch (Exception e) {  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59.     /** 
  60.      * Disconnect with server 
  61.      */  
  62.     public void disconnect() {  
  63.         if(this.sftp != null){  
  64.             if(this.sftp.isConnected()){  
  65.                 this.sftp.disconnect();  
  66.             }else if(this.sftp.isClosed()){  
  67.                 System.out.println("sftp is closed already");  
  68.             }  
  69.         }  
  70.   
  71.     }  
  72.   
  73.     public void download() {  
  74.         // TODO Auto-generated method stub  
  75.           
  76.   
  77.     }  
  78.       
  79.       
  80.     private void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp) {  
  81.         try {  
  82.             sftp.cd(directory);  
  83.             File file = new File(saveFile);  
  84.             sftp.get(downloadFile, new FileOutputStream(file));  
  85.         } catch (Exception e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.     }  
  89.       
  90.     /** 
  91.      * upload all the files to the server 
  92.      */  
  93.     public void upload() {  
  94.         List<String> fileList = this.getFileEntryList(fileListPath);  
  95.         try {  
  96.             if(fileList != null){  
  97.                 for (String filepath : fileList) {  
  98.                     String localFile = this.localPath + this.seperator+ filepath;  
  99.                     File file = new File(localFile);  
  100.                       
  101.                     if(file.isFile()){  
  102.                         System.out.println("localFile : " + file.getAbsolutePath());  
  103.                         String remoteFile = this.remotePath + this.seperator + filepath;  
  104.                         System.out.println("remotePath:" + remoteFile);  
  105.                         File rfile = new File(remoteFile);  
  106.                         String rpath = rfile.getParent();  
  107.                         try {  
  108.                             createDir(rpath, sftp);  
  109.                         } catch (Exception e) {  
  110.                             System.out.println("*******create path failed" + rpath);  
  111.                         }  
  112.   
  113.                         this.sftp.put(new FileInputStream(file), file.getName());  
  114.                         System.out.println("=========upload down for " + localFile);  
  115.                     }  
  116.                 }  
  117.             }  
  118.         } catch (FileNotFoundException e) {  
  119.             // TODO Auto-generated catch block  
  120.             e.printStackTrace();  
  121.         } catch (SftpException e) {  
  122.             // TODO Auto-generated catch block  
  123.             e.printStackTrace();  
  124.         }  
  125.           
  126.     }  
  127.     /** 
  128.      * create Directory 
  129.      * @param filepath 
  130.      * @param sftp 
  131.      */  
  132.     private void createDir(String filepath, ChannelSftp sftp){  
  133.         boolean bcreated = false;  
  134.         boolean bparent = false;  
  135.         File file = new File(filepath);  
  136.         String ppath = file.getParent();  
  137.         try {  
  138.             this.sftp.cd(ppath);  
  139.             bparent = true;  
  140.         } catch (SftpException e1) {  
  141.             bparent = false;  
  142.         }  
  143.         try {  
  144.             if(bparent){  
  145.                 try {  
  146.                     this.sftp.cd(filepath);  
  147.                     bcreated = true;  
  148.                 } catch (Exception e) {  
  149.                     bcreated = false;  
  150.                 }  
  151.                 if(!bcreated){  
  152.                     this.sftp.mkdir(filepath);  
  153.                     bcreated = true;  
  154.                 }  
  155.                 return;  
  156.             }else{  
  157.                 createDir(ppath,sftp);  
  158.                 this.sftp.cd(ppath);  
  159.                 this.sftp.mkdir(filepath);  
  160.             }  
  161.         } catch (SftpException e) {  
  162.             System.out.println("mkdir failed :" + filepath);  
  163.             e.printStackTrace();  
  164.         }  
  165.           
  166.         try {  
  167.             this.sftp.cd(filepath);  
  168.         } catch (SftpException e) {  
  169.             e.printStackTrace();  
  170.             System.out.println("can not cd into :" + filepath);  
  171.         }  
  172.           
  173.     }  
  174.     /** 
  175.      * get all the files need to be upload or download 
  176.      * @param file 
  177.      * @return 
  178.      */  
  179.     private List<String> getFileEntryList(String file){  
  180.         ArrayList<String> fileList = new ArrayList<String>();  
  181.         InputStream in = null;  
  182.         try {  
  183.               
  184.             in = new FileInputStream(file);  
  185.             InputStreamReader inreader = new InputStreamReader(in);  
  186.               
  187.             LineNumberReader linreader = new LineNumberReader(inreader);  
  188.             String filepath = linreader.readLine();  
  189.             while(filepath != null){  
  190.                 fileList.add(filepath);  
  191.                 filepath = linreader.readLine();  
  192.             }  
  193.             in.close();  
  194.         } catch (FileNotFoundException e) {  
  195.             // TODO Auto-generated catch block  
  196.             e.printStackTrace();  
  197.         } catch (IOException e) {  
  198.             // TODO Auto-generated catch block  
  199.             e.printStackTrace();  
  200.         }finally{  
  201.             if(in != null){  
  202.                 in = null;  
  203.             }  
  204.         }  
  205.   
  206.         return fileList;  
  207.     }  
  208.   
  209.     /** 
  210.      * @return the host 
  211.      */  
  212.     public String getHost() {  
  213.         return host;  
  214.     }  
  215.   
  216.     /** 
  217.      * @param host the host to set 
  218.      */  
  219.     public void setHost(String host) {  
  220.         this.host = host;  
  221.     }  
  222.   
  223.     /** 
  224.      * @return the username 
  225.      */  
  226.     public String getUsername() {  
  227.         return username;  
  228.     }  
  229.   
  230.     /** 
  231.      * @param username the username to set 
  232.      */  
  233.     public void setUsername(String username) {  
  234.         this.username = username;  
  235.     }  
  236.   
  237.     /** 
  238.      * @return the password 
  239.      */  
  240.     public String getPassword() {  
  241.         return password;  
  242.     }  
  243.   
  244.     /** 
  245.      * @param password the password to set 
  246.      */  
  247.     public void setPassword(String password) {  
  248.         this.password = password;  
  249.     }  
  250.   
  251.     /** 
  252.      * @return the port 
  253.      */  
  254.     public int getPort() {  
  255.         return port;  
  256.     }  
  257.   
  258.     /** 
  259.      * @param port the port to set 
  260.      */  
  261.     public void setPort(int port) {  
  262.         this.port = port;  
  263.     }  
  264.   
  265.     /** 
  266.      * @return the sftp 
  267.      */  
  268.     public ChannelSftp getSftp() {  
  269.         return sftp;  
  270.     }  
  271.   
  272.     /** 
  273.      * @param sftp the sftp to set 
  274.      */  
  275.     public void setSftp(ChannelSftp sftp) {  
  276.         this.sftp = sftp;  
  277.     }  
  278.   
  279.     /** 
  280.      * @return the localPath 
  281.      */  
  282.     public String getLocalPath() {  
  283.         return localPath;  
  284.     }  
  285.   
  286.     /** 
  287.      * @param localPath the localPath to set 
  288.      */  
  289.     public void setLocalPath(String localPath) {  
  290.         this.localPath = localPath;  
  291.     }  
  292.   
  293.     /** 
  294.      * @return the remotePath 
  295.      */  
  296.     public String getRemotePath() {  
  297.         return remotePath;  
  298.     }  
  299.   
  300.     /** 
  301.      * @param remotePath the remotePath to set 
  302.      */  
  303.     public void setRemotePath(String remotePath) {  
  304.         this.remotePath = remotePath;  
  305.     }  
  306.   
  307.     /** 
  308.      * @return the fileListPath 
  309.      */  
  310.     public String getFileListPath() {  
  311.         return fileListPath;  
  312.     }  
  313.   
  314.     /** 
  315.      * @param fileListPath the fileListPath to set 
  316.      */  
  317.     public void setFileListPath(String fileListPath) {  
  318.         this.fileListPath = fileListPath;  
  319.     }  
  320.       
  321.     public static void main(String[] args) {  
  322.         // TODO Auto-generated method stub  
  323.         SFTPUtil ftp= new SFTPUtil();  
  324.         ftp.connect();  
  325.         ftp.upload();  
  326.         ftp.disconnect();  
  327.         System.exit(0);  
  328.     }  
  329.   
  330.   
  331. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值