Guest User

Untitled

a guest
Aug 10th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.OutputStream;
  7.  
  8. import com.jcraft.jsch.Channel;
  9. import com.jcraft.jsch.ChannelSftp;
  10. import com.jcraft.jsch.JSch;
  11. import com.jcraft.jsch.JSchException;
  12. import com.jcraft.jsch.Session;
  13.  
  14. public class FTPExample {
  15.  
  16. private String host;
  17. private Integer port;
  18. private String user;
  19. private String password;
  20.  
  21. private JSch jsch;
  22. private Session session;
  23. private Channel channel;
  24. private ChannelSftp sftpChannel;
  25.  
  26. public FTPExample(String host, Integer port, String user, String password) {
  27. this.host = host;
  28. this.port = port;
  29. this.user = user;
  30. this.password = password;
  31. }
  32.  
  33. public void connect() {
  34.  
  35. System.out.println("connecting..."+host);
  36. try {
  37. jsch = new JSch();
  38. session = jsch.getSession(user, host,port);
  39. session.setConfig("StrictHostKeyChecking", "no");
  40. session.setPassword(password);
  41. session.connect();
  42.  
  43. channel = session.openChannel("sftp");
  44. channel.connect();
  45. sftpChannel = (ChannelSftp) channel;
  46.  
  47. } catch (JSchException e) {
  48. e.printStackTrace();
  49. }
  50.  
  51. }
  52.  
  53. public void disconnect() {
  54. System.out.println("disconnecting...");
  55. sftpChannel.disconnect();
  56. channel.disconnect();
  57. session.disconnect();
  58. }
  59.  
  60. public void upload(String fileName, String remoteDir) {
  61.  
  62. FileInputStream fis = null;
  63. connect();
  64. try {
  65. // Change to output directory
  66. sftpChannel.cd(remoteDir);
  67.  
  68. // Upload file
  69. File file = new File(fileName);
  70. fis = new FileInputStream(file);
  71. sftpChannel.put(fis, file.getName());
  72.  
  73. fis.close();
  74. System.out.println("File uploaded successfully - "+ file.getAbsolutePath());
  75.  
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. disconnect();
  80. }
  81.  
  82. public void download(String fileName, String localDir) {
  83.  
  84. byte[] buffer = new byte[1024];
  85. BufferedInputStream bis;
  86. connect();
  87. try {
  88. // Change to output directory
  89. String cdDir = fileName.substring(0, fileName.lastIndexOf("/") + 1);
  90. sftpChannel.cd(cdDir);
  91.  
  92. File file = new File(fileName);
  93. bis = new BufferedInputStream(sftpChannel.get(file.getName()));
  94.  
  95. File newFile = new File(localDir + "/" + file.getName());
  96.  
  97. // Download file
  98. OutputStream os = new FileOutputStream(newFile);
  99. BufferedOutputStream bos = new BufferedOutputStream(os);
  100. int readCount;
  101. while ((readCount = bis.read(buffer)) > 0) {
  102. bos.write(buffer, 0, readCount);
  103. }
  104. bis.close();
  105. bos.close();
  106. System.out.println("File downloaded successfully - "+ file.getAbsolutePath());
  107.  
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. }
  111. disconnect();
  112. }
  113.  
  114. public static void main(String[] args) {
  115.  
  116.  
  117. String localPath = "C:/temp/";
  118. String remotePath = "/export/home/madan/";
  119.  
  120. FTPExample ftp = new FTPExample("10.100.12.14", 2222, "madan", "password");
  121.  
  122. ftp.upload(localPath+"filetoupload.txt", remotePath);
  123.  
  124. ftp.download(remotePath+"filetodownload.txt", localPath);
  125.  
  126. }
  127.  
  128. }
Add Comment
Please, Sign In to add comment