天天看點

使用jsch實作檔案上傳

  1. import java.io.File;   
  2. import java.io.FileInputStream;   
  3. import java.io.IOException;   
  4. import java.io.InputStream;   
  5. import java.io.OutputStream;   
  6. import java.util.ArrayList;   
  7. import java.util.List;   
  8. import java.util.Vector;   
  9. import com.jcraft.jsch.Channel;   
  10. import com.jcraft.jsch.ChannelSftp;   
  11. import com.jcraft.jsch.JSch;   
  12. import com.jcraft.jsch.JSchException;   
  13. import com.jcraft.jsch.Session;   
  14. import com.jcraft.jsch.SftpException;   
  15. import com.shonetown.common.util.log.EventLog;   
  16. public class SftpHelper extends Thread {   
  17.     private static EventLog log = new EventLog(SftpHelper.class);   
  18.     private String host;   
  19.     private String username;   
  20.     private String password;   
  21.     private String location;   
  22.     private int port;   
  23.     private String knowHosts;   
  24.     private String osName;   
  25.     private List<String> filenames = new ArrayList<String>();   
  26.     public SftpHelper(String host, String username, String password, int port) {   
  27.         this(host, username, password, port, "");   
  28.     }   
  29.     public SftpHelper(String host, String username, String password, int port, String location) {   
  30.         this.host = host;   
  31.         this.username = username;   
  32.         this.password = password;   
  33.         this.port = port;   
  34.         osName = System.getProperty("os.name");   
  35.         if (osName.toUpperCase().indexOf("WINDOWS") > -1) {   
  36.             this.knowHosts = "c://known_hosts";   
  37.             if(location == null || location.length() == 0){   
  38.                 this.location = "c://";   
  39.             }   
  40.         } else {   
  41.             this.knowHosts = "/root/.ssh/known_hosts";   
  42.             if(location == null || location.length() == 0){   
  43.                 this.location = "/";   
  44.             }   
  45.         }   
  46.         this.location = location;   
  47.     }   
  48.     public void addFilename(String filename){   
  49.         filenames.add(filename);   
  50.     }   
  51.     public void setFilenames(List<String> filenames){   
  52.         this.filenames = filenames;   
  53.     }   
  54.     public void run(){   
  55.         upload();   
  56.     }   
  57.     public boolean upload(){   
  58.         if(filenames.size() == 0)   
  59.             return false;   
  60.         Session session;   
  61.         Channel channel;   
  62.         JSch jsch = new JSch();   
  63.         try {   
  64.             jsch.setKnownHosts(knowHosts);   
  65.             session = jsch.getSession(username, host, port);   
  66.             session.setPassword(password);   
  67.             session.connect();   
  68.             channel = session.openChannel("sftp");   
  69.             channel.connect();   
  70.             ChannelSftp c = (ChannelSftp)channel;   
  71.             c.cd(location);   
  72.             InputStream in = null;   
  73.             OutputStream out = null;   
  74.             for(int i=0; i<filenames.size(); i++){   
  75.                 String filename = filenames.get(i);   
  76.                 if(filename == null || "".equals(filename)){   
  77.                     log.Debug("", "目前沒有要上傳的檔案!");   
  78.                     continue;   
  79.                 }   
  80.                 int idx= filename.lastIndexOf(File.separator);   
  81.                 String uploadname = filename.substring(idx==-1?0:idx+1);   
  82.                 out = c.put(uploadname);   
  83.                 log.Debug("", "sleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep "+5000+"ms!");   
  84.                 sleep(5000);   
  85.                 in = new FileInputStream(filename);   
  86. //              String suffix = filename.substring(filename.lastIndexOf(".")+1);   
  87. //              if("gz".equals(suffix)){   
  88. //                  in = new GZIPInputStream(in);   
  89. //              }   
  90.                 byte [] b = new byte[1024];   
  91.                 int n;   
  92.                 while ((n = in.read(b)) != -1) {   
  93.                     out.write(b);   
  94.                 }   
  95.             }   
  96.             out.flush();   
  97.             out.close();   
  98.             in.close();   
  99.             c.disconnect();   
  100.             session.disconnect();   
  101.             sleep(500);   
  102.             return true;   
  103.         } catch (JSchException e) {   
  104.             e.printStackTrace();   
  105.         } catch (SftpException e) {   
  106.             e.printStackTrace();   
  107.         } catch (IOException e) {   
  108.             e.printStackTrace();   
  109.         } catch(InterruptedException e){   
  110.             e.printStackTrace();   
  111.         }   
  112.         return false;   
  113.     }   
  114.     public static void main(String[] args){   
  115.         String username = "root";   
  116.         String host = "*.*.*.*";   
  117.         int port = 22;   
  118.         String password = "******";   
  119.         String path = "/home/data/download/";   
  120.         SftpHelper helper = new SftpHelper(host, username,password, port,path);   
  121.         helper.addFilename("c://bcp.sql");   
  122.         helper.addFilename("c://a.sql");   
  123. //      helper.upload("c://bcp.sql");   
  124.         helper.start();   
  125.     }   
  126. }