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

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