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