Guest User

Untitled

a guest
Oct 17th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. public class TestJSch {
  2.  
  3. /** Creates a new instance of TestCommonsNet */
  4. public TestJSch() {
  5. }
  6.  
  7. /**
  8. * main - Unit test program
  9. *
  10. * @param args
  11. * Command line arguments
  12. *
  13. */
  14. public static void main(String[] args) {
  15. try {
  16. String ftpHost = "127.0.0.1";
  17. int ftpPort = 21;// 14147;
  18. // int ftpPort = 990;// 14147;
  19. String ftpUserName = "kedar";
  20. String ftpPassword = "XXXXXXXXXXX";
  21. String ftpRemoteDirectory = "C:\KEDAR\Java\FTP_Folder";
  22. String fileToTransmit = "C:\KEDAR\Java\File_Folder\Customer.txt";
  23. String identityfile = "C:\KEDAR\Java\Ftp\certificate.crt";
  24.  
  25. //
  26. // First Create a JSch session
  27. //
  28. JSch.setLogger(new MyLogger());
  29. System.out.println("Creating session.");
  30.  
  31. JSch jsch = new JSch();
  32.  
  33. String knownHostsFilename = "C:\Windows\System32\drivers\etc\hosts";
  34. jsch.setKnownHosts(knownHostsFilename);
  35. jsch.addIdentity(identityfile);
  36. Session session = null;
  37. Channel channel = null;
  38. ChannelSftp c = null;
  39.  
  40. //
  41. // Now connect and SFTP to the SFTP Server
  42. //
  43. try {
  44. // Create a session sending through our username and password
  45. session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
  46. System.out.println("Session created.");
  47. session.setPassword(ftpPassword);
  48. // Security.addProvider(new com.sun.crypto.provider.SunJCE());
  49.  
  50. // b
  51. // Setup Strict HostKeyChecking to no so we dont get the
  52. // unknown host key exception
  53. //
  54. java.util.Properties config = new java.util.Properties();
  55. config.put("StrictHostKeyChecking", "no");
  56. session.setConfig(config);
  57. session.connect();
  58. System.out.println("Session connected.");
  59.  
  60. //
  61. // Open the SFTP channel
  62. //
  63. System.out.println("Opening Channel.");
  64. channel = session.openChannel("sftp");
  65. channel.connect();
  66. c = (ChannelSftp) channel;
  67. } catch (Exception e) {
  68. System.err.println("Unable to connect to FTP server."
  69. + e.toString());
  70. throw e;
  71. }
  72.  
  73. //
  74. // Change to the remote directory
  75. //
  76. System.out.println("Changing to FTP remote dir: "
  77. + ftpRemoteDirectory);
  78. c.cd(ftpRemoteDirectory);
  79.  
  80. //
  81. // Send the file we generated
  82. //
  83. try {
  84. File f = new File(fileToTransmit);
  85. System.out.println("Storing file as remote filename: "
  86. + f.getName());
  87. c.put(new FileInputStream(f), f.getName());
  88. } catch (Exception e) {
  89. System.err
  90. .println("Storing remote file failed." + e.toString());
  91. throw e;
  92. }
  93.  
  94. //
  95. // Disconnect from the FTP server
  96. //
  97. try {
  98. c.quit();
  99. } catch (Exception exc) {
  100. System.err.println("Unable to disconnect from FTPserver. "
  101. + exc.toString());
  102. }
  103.  
  104. } catch (Exception e) {
  105. System.err.println("Error: " + e.toString());
  106. }
  107.  
  108. System.out.println("Process Complete.");
  109. System.exit(0);
  110. }
  111.  
  112. public static class MyLogger implements com.jcraft.jsch.Logger {
  113. static java.util.Hashtable name = new java.util.Hashtable();
  114. static {
  115. name.put(new Integer(DEBUG), "DEBUG: ");
  116. name.put(new Integer(INFO), "INFO: ");
  117. name.put(new Integer(WARN), "WARN: ");
  118. name.put(new Integer(ERROR), "ERROR: ");
  119. name.put(new Integer(FATAL), "FATAL: ");
  120. }
  121.  
  122. public boolean isEnabled(int level) {
  123. return true;
  124. }
  125.  
  126. public void log(int level, String message) {
  127. System.err.print(name.get(new Integer(level)));
  128. System.err.println(message);
  129. }
  130. }
  131. }
  132.  
  133. import java.io.IOException;
  134.  
  135. import org.apache.commons.net.ftp.FTPClient;
  136.  
  137. import org.apache.commons.net.ftp.FTPReply;
  138.  
  139. public class FTPConnectionCode {
  140.  
  141. public static void main(String[] args) {
  142. String server = "www.website.com";
  143. // generally ftp port is 21
  144. int port = 21;
  145. String user = "ftpusername";
  146. String pass = "ftppassword";
  147.  
  148. FTPClient ftpClient = new FTPClient();
  149.  
  150. try {
  151.  
  152. ftpClient.connect(server, port);
  153. showServerReply(ftpClient);
  154.  
  155. int replyCode = ftpClient.getReplyCode();
  156. if (!FTPReply.isPositiveCompletion(replyCode)) {
  157. System.out.println("Connect failed");
  158. return;
  159. }
  160.  
  161. boolean success = ftpClient.login(user, pass);
  162. showServerReply(ftpClient);
  163.  
  164. if (!success) {
  165. System.out.println("Could not login to the server");
  166. return;
  167. }
  168.  
  169. // Changes working directory
  170. success = ftpClient.changeWorkingDirectory("/dir");
  171. showServerReply(ftpClient);
  172.  
  173. if (success) {
  174. System.out.println("Successfully changed working directory.");
  175. } else {
  176. System.out.println("Failed to change working directory. See server's reply.");
  177. }
  178.  
  179. // logs out
  180. ftpClient.logout();
  181. ftpClient.disconnect();
  182.  
  183. } catch (IOException ex) {
  184. System.out.println("Oops! Something wrong happened");
  185. ex.printStackTrace();
  186. }
  187. }
  188.  
  189. private static void showServerReply(FTPClient ftpClient) {
  190. String[] replies = ftpClient.getReplyStrings();
  191. if (replies != null && replies.length > 0) {
  192. for (String aReply : replies) {
  193. System.out.println("SERVER: " + aReply);
  194. }
  195. }
  196. }
  197. }
Add Comment
Please, Sign In to add comment