Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. public static void sftpConnection() {
  2.  
  3. // Object Declaration.
  4. JSch jsch = new JSch();
  5. Session session = null;
  6. Channel channel = null;
  7.  
  8. // Variable Declaration.
  9. String user = "foo";
  10. String host = "10.9.8.7";
  11. Integer port = 22;
  12. String password = "test123";
  13. String watchFolder = "\localhosttextfiles";
  14. String outputDir = "/remote/textFolder/";
  15. String filemask = "*.txt";
  16.  
  17.  
  18. try {
  19. session = jsch.getSession(user, host, port);
  20.  
  21. /*
  22. * StrictHostKeyChecking Indicates what to do if the server's host
  23. * key changed or the server is unknown. One of yes (refuse connection),
  24. * ask (ask the user whether to add/change the key) and no
  25. * (always insert the new key).
  26. */
  27. session.setConfig("StrictHostKeyChecking", "no");
  28. session.setPassword(password);
  29.  
  30. session.connect();
  31.  
  32. channel = session.openChannel("sftp");
  33. channel.connect();
  34. ChannelSftp sftpChannel = (ChannelSftp)channel;
  35.  
  36. // Go through watch folder looking for files.
  37. File[] files = findFile(watchFolder, filemask);
  38. for(File file : files) {
  39. // Upload file.
  40. putFile(file, sftpChannel, outputDir);
  41. }
  42. } finally {
  43. sftpChannel.exit();
  44. session.disconnect();
  45. }
  46. }
  47.  
  48. public static void putFile(File file, ChannelSftp sftpChannel, String outputDir) {
  49.  
  50. FileInputStream fis = null;
  51.  
  52. try {
  53. // Change to output directory.
  54. sftpChannel.cd(outputDir);
  55.  
  56. // Upload file.
  57.  
  58. fis = new FileInputStream(file);
  59. sftpChannel.put(fis, file.getName());
  60. fis.close();
  61.  
  62. } catch{}
  63. }
  64.  
  65. public static File[] findFile(String dirName, final String mask) {
  66. File dir = new File(dirName);
  67.  
  68. return dir.listFiles(new FilenameFilter() {
  69. public boolean accept(File dir, String filename)
  70. { return filename.endsWith(mask); }
  71. } );
  72. }
  73.  
  74. private static final String USER_PROMPT = "Enter username@hostname:port";
  75. private static final boolean USE_GUI = true;
  76.  
  77. public static void main(final String[] arg) {
  78. Session session = null;
  79. ChannelSftp channelSftp = null;
  80. try {
  81. final JSch jsch = new JSch();
  82.  
  83. final String defaultInput = System.getProperty("user.name") + "@localhost:22";
  84. String input = (USE_GUI) ? JOptionPane.showInputDialog(USER_PROMPT, defaultInput) : System.console().readLine("%s (%s): ", USER_PROMPT, defaultInput);
  85. if (input == null || input.trim().length() == 0) {
  86. input = defaultInput;
  87. }
  88. final int indexOfAt = input.indexOf('@');
  89. final int indexOfColon = input.indexOf(':');
  90. final String user = input.substring(0, indexOfAt);
  91. final String host = input.substring(indexOfAt + 1, indexOfColon);
  92. final int port = Integer.parseInt(input.substring(indexOfColon + 1));
  93.  
  94. jsch.setKnownHosts("/path/to/known_hosts");
  95. // if you have set up authorized_keys on the server, using that identitiy
  96. // with the code on the next line allows for password-free, trusted connections
  97. // jsch.addIdentity("/path/to/id_rsa", "id_rsa_password");
  98.  
  99. session = jsch.getSession(user, host, 22);
  100.  
  101. final UserInfo ui = new MyUserInfo();
  102. session.setUserInfo(ui);
  103. session.connect();
  104. channelSftp = (ChannelSftp) session.openChannel("sftp");
  105. channelSftp.connect();
  106. channelSftp.get("/remotepath/remotefile.txt", "/localpath/localfile.txt");
  107. } finally {
  108. if (channelSftp != null) {
  109. channelSftp.exit();
  110. }
  111. if (session != null) {
  112. session.disconnect();
  113. }
  114. }
  115. }
  116.  
  117. public static class MyUserInfo implements UserInfo {
  118. private String password;
  119.  
  120. @Override
  121. public String getPassword() {
  122. return password;
  123. }
  124.  
  125. @Override
  126. public boolean promptYesNo(final String str) {
  127. final Object[] options = {"yes", "no"};
  128. final boolean yesNo = (USE_GUI) ? JOptionPane.showOptionDialog(null, str, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]) == 0 : System.console().readLine("Enter y or n: ").equals("y");
  129. return yesNo;
  130. }
  131.  
  132. @Override
  133. public String getPassphrase() {
  134. return null;
  135. }
  136.  
  137. @Override
  138. public boolean promptPassphrase(final String message) {
  139. return true;
  140. }
  141.  
  142. @Override
  143. public boolean promptPassword(final String message) {
  144. if (!USE_GUI) {
  145. password = new String(System.console().readPassword("Password: "));
  146. return true;
  147. } else {
  148. final JTextField passwordField = new JPasswordField(20);
  149. final Object[] ob = {passwordField};
  150. final int result = JOptionPane.showConfirmDialog(null, ob, message, JOptionPane.OK_CANCEL_OPTION);
  151. if (result == JOptionPane.OK_OPTION) {
  152. password = passwordField.getText();
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. }
  159.  
  160. @Override
  161. public void showMessage(final String message) {
  162. if (!USE_GUI) {
  163. System.console().printf(message);
  164. } else {
  165. JOptionPane.showMessageDialog(null, message);
  166. }
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement