- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Vector;
- import com.jcraft.jsch.Channel;
- import com.jcraft.jsch.ChannelSftp;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.JSchException;
- import com.jcraft.jsch.Session;
- import com.jcraft.jsch.SftpException;
- import com.shonetown.common.util.log.EventLog;
- public class SftpHelper extends Thread {
- private static EventLog log = new EventLog(SftpHelper.class);
- private String host;
- private String username;
- private String password;
- private String location;
- private int port;
- private String knowHosts;
- private String osName;
- private List<String> filenames = new ArrayList<String>();
- public SftpHelper(String host, String username, String password, int port) {
- this(host, username, password, port, "");
- }
- public SftpHelper(String host, String username, String password, int port, String location) {
- this.host = host;
- this.username = username;
- this.password = password;
- this.port = port;
- osName = System.getProperty("os.name");
- if (osName.toUpperCase().indexOf("WINDOWS") > -1) {
- this.knowHosts = "c://known_hosts";
- if(location == null || location.length() == 0){
- this.location = "c://";
- }
- } else {
- this.knowHosts = "/root/.ssh/known_hosts";
- if(location == null || location.length() == 0){
- this.location = "/";
- }
- }
- this.location = location;
- }
- public void addFilename(String filename){
- filenames.add(filename);
- }
- public void setFilenames(List<String> filenames){
- this.filenames = filenames;
- }
- public void run(){
- upload();
- }
- public boolean upload(){
- if(filenames.size() == 0)
- return false;
- Session session;
- Channel channel;
- JSch jsch = new JSch();
- try {
- jsch.setKnownHosts(knowHosts);
- session = jsch.getSession(username, host, port);
- session.setPassword(password);
- session.connect();
- channel = session.openChannel("sftp");
- channel.connect();
- ChannelSftp c = (ChannelSftp)channel;
- c.cd(location);
- InputStream in = null;
- OutputStream out = null;
- for(int i=0; i<filenames.size(); i++){
- String filename = filenames.get(i);
- if(filename == null || "".equals(filename)){
- log.Debug("", "目前沒有要上傳的檔案!");
- continue;
- }
- int idx= filename.lastIndexOf(File.separator);
- String uploadname = filename.substring(idx==-1?0:idx+1);
- out = c.put(uploadname);
- log.Debug("", "sleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep "+5000+"ms!");
- sleep(5000);
- in = new FileInputStream(filename);
- // String suffix = filename.substring(filename.lastIndexOf(".")+1);
- // if("gz".equals(suffix)){
- // in = new GZIPInputStream(in);
- // }
- byte [] b = new byte[1024];
- int n;
- while ((n = in.read(b)) != -1) {
- out.write(b);
- }
- }
- out.flush();
- out.close();
- in.close();
- c.disconnect();
- session.disconnect();
- sleep(500);
- return true;
- } catch (JSchException e) {
- e.printStackTrace();
- } catch (SftpException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch(InterruptedException e){
- e.printStackTrace();
- }
- return false;
- }
- public static void main(String[] args){
- String username = "root";
- String host = "*.*.*.*";
- int port = 22;
- String password = "******";
- String path = "/home/data/download/";
- SftpHelper helper = new SftpHelper(host, username,password, port,path);
- helper.addFilename("c://bcp.sql");
- helper.addFilename("c://a.sql");
- // helper.upload("c://bcp.sql");
- helper.start();
- }
- }