1 package com.imix.sftp; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.util.Properties; 8 import java.util.Vector; 9 10 import com.jcraft.jsch.Channel; 11 import com.jcraft.jsch.ChannelSftp; 12 import com.jcraft.jsch.JSch; 13 import com.jcraft.jsch.JSchException; 14 import com.jcraft.jsch.Session; 15 import com.jcraft.jsch.SftpException; 16 17 18 /** 19 * @ClassName: SFTP 20 * @Author: Ares 21 * @Description: TODO(sftp demo) 22 * @Date: 2013-3-1 下午12:25:24 23 */ 24 public class SFTP { 25 26 /** 27 * @param args 28 */ 29 public static void main(String[] args) { 30 31 SFTP ftp = new SFTP(); 32 String host="200.31.154.19"; 33 int port = 22; 34 String username="root"; 35 String password="123456"; 36 String directory="/home/imix/"; 37 String uploadFile = "D:/ipconfig.txt"; 38 String downloadFile = "ipconfig.txt"; 39 String saveFile = "D:/ipconfig.txt"; 40 String deleteFile = "ipconfig.txt"; 41 42 ChannelSftp sftp = ftp.connect(host, port, username, password); 43 ftp.upload(directory, uploadFile, sftp); 44 ftp.download(directory, downloadFile, saveFile, sftp); 45 ftp.delete(directory, deleteFile, sftp); 46 try { 47 sftp.cd(directory); 48 sftp.mkdir("createFolder"); 49 } catch (SftpException e) { 50 e.printStackTrace(); 51 }finally{ 52 try { 53 //如果没有sesstion的disconnect,程序不会退出 54 sftp.getSession().disconnect(); 55 } catch (JSchException e) { 56 e.printStackTrace(); 57 } 58 sftp.disconnect(); 59 sftp.exit(); 60 } 61 } 62 63 /** 64 * @AddBy: Ares 65 * @Description: TODO(connect the host) 66 * @param host 67 * @param port 68 * @param username 69 * @param password 70 * @return 71 */ 72 public ChannelSftp connect(String host, int port, String username, String password) { 73 ChannelSftp csftp = null; 74 JSch jsch = new JSch(); 75 try { 76 Session sshSession = jsch.getSession(username, host, port); 77 System.out.println("jsch session created, user="+username); 78 79 sshSession.setPassword(password); 80 Properties sshConfig = new Properties(); 81 sshConfig.put("StrictHostKeyChecking", "no"); 82 sshSession.setConfig(sshConfig); 83 sshSession.connect(); 84 System.out.println("session is connected."); 85 86 Channel channel = sshSession.openChannel("sftp"); 87 channel.connect(); 88 89 csftp = (ChannelSftp)channel; 90 System.out.println("connected to host:"+ host); 91 92 } catch (JSchException e) { 93 e.printStackTrace(); 94 } 95 return csftp; 96 } 97 98 /** 99 * @AddBy: Ares 100 * @Description: TODO(upload file to host) 101 * @param directory 102 * @param uploadFile 103 * @param sftp 104 * @return 105 */ 106 public boolean upload(String directory, String uploadFile, ChannelSftp sftp){ 107 File file = new File(uploadFile); 108 try { 109 sftp.cd(directory); 110 sftp.put(new FileInputStream(file), file.getName()); 111 System.out.println("upload file success, file:"+uploadFile); 112 } catch (Exception e) { 113 System.err.println("upload file failed, file:"+uploadFile); 114 e.printStackTrace(); 115 return false; 116 } 117 return true; 118 } 119 /** 120 * @AddBy: Ares 121 * @Description: TODO(download file from host) 122 * @param directory 123 * @param downloadFile 124 * @param saveFile 125 * @param sftp 126 * @return 127 */ 128 public boolean download(String directory, String downloadFile, String saveFile, ChannelSftp sftp){ 129 File file = new File(saveFile); 130 try { 131 sftp.cd(directory); 132 sftp.get(downloadFile, new FileOutputStream(file)); 133 System.out.println("download file success, file:"+downloadFile); 134 } catch (FileNotFoundException e) { 135 e.printStackTrace(); 136 return false; 137 } catch (SftpException e) { 138 System.err.println("download file failed, file:"+downloadFile); 139 e.printStackTrace(); 140 return false; 141 } 142 return true; 143 } 144 145 /** 146 * @AddBy: Ares 147 * @Description: TODO(delete file of host) 148 * @param directory 149 * @param deleteFile 150 * @param sftp 151 * @return 152 */ 153 public boolean delete(String directory, String deleteFile, ChannelSftp sftp){ 154 try { 155 sftp.cd(directory); 156 sftp.rm(deleteFile); 157 System.out.println("delete file success, file:"+deleteFile); 158 } catch (SftpException e) { 159 System.err.println("delete file failed, file:"+deleteFile); 160 e.printStackTrace(); 161 return false; 162 } 163 return true; 164 } 165 166 /** 167 * @AddBy: Ares 168 * @Description: TODO(get file list from directory of host) 169 * @param directory 170 * @param sftp 171 * @return 172 */ 173 public Vector<?> listFiles(String directory, ChannelSftp sftp){ 174 try { 175 return sftp.ls(directory); 176 } catch (SftpException e) { 177 System.err.println("list directory failed, directory:"+directory); 178 e.printStackTrace(); 179 } 180 return null; 181 } 182 183 }