Com.jcraft.jsch.ChannelSftp explained in Java

Source: Internet
Author: User
Tags file transfer protocol

http://blog.csdn.net/allen_zhao_2012/article/details/7941631

Http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html

http://xpenxpen.iteye.com/blog/2061869

http://blog.csdn.net/fyqcdbdx/article/details/23863793

Http://blog.csdn.net/u013256816/article/details/52701563?utm_source=gold_browser_extension

SFTP is the abbreviation for Secure File Transfer protocol, security File Transfer Protocol. You can provide a secure encryption method for transferring files. SFTP is a part of SSH and is a secure way to transfer files to the server. SFTP is the use of encrypted transmission of authentication information and transmitted data, so the use of SFTP is very safe. However, because this transmission uses the encryption/decryption technology, the transfer efficiency is much lower than the normal FTP, and if you have higher network security requirements, you can use SFTP instead of FTP.

The Channelsftp class is the Jsch implementation of the SFTP core class, which contains all of the sftp methods, such as: Put (): File Upload get (): File download CD (): Enter the specified directory ls (): Get a list of files in the specified directory Renam E (): Rename the specified file or directory rm (): Delete the specified file mkdir (): Create Directory RmDir (): Delete directory and so on (here omit the method parameters, put and get have multiple overloaded methods, see the source code, not listed here. )

A simple Jsch link to Linux and execute the utils of the command.

  1. Import Java.io.BufferedReader;
  2. Import java.io.IOException;
  3. Import Java.io.InputStream;
  4. Import Java.io.InputStreamReader;
  5. Import Com.jcraft.jsch.Channel;
  6. Import com.jcraft.jsch.ChannelExec;
  7. Import Com.jcraft.jsch.JSch;
  8. Import com.jcraft.jsch.JSchException;
  9. Import com.jcraft.jsch.Session;
  10. Public class Shellutils {
  11. private static Jsch Jsch;
  12. private static session session;
  13. /** 
  14. * Connect to the specified IP
  15. *
  16. * @throws jschexception
  17. */
  18. public static void Connect (string user, String passwd, string host) throws jschexception {
  19. Jsch = new Jsch ();
  20. Session = jsch.getsession (user, host, 22);
  21. Session.setpassword (passwd);
  22. java.util.Properties config = new java.util.Properties ();
  23. Config.put ("stricthostkeychecking", "no");
  24. Session.setconfig (config);
  25. Session.connect ();
  26. }
  27. /** 
  28. * Execution of RELATED commands
  29. * @throws jschexception
  30. */
  31. public static void ExecCmd (String command, string user, String passwd, string host) throws Jschexception {
  32. Connect (user, passwd, host);
  33. BufferedReader reader = null;
  34. Channel channel = null;
  35. try {
  36. While (Command! = null) {
  37. Channel = Session.openchannel ("exec");
  38. ((channelexec) channel). SetCommand (command);
  39. Channel.setinputstream (null);
  40. ((channelexec) channel). Seterrstream (System.err);
  41. Channel.connect ();
  42. InputStream in = Channel.getinputstream ();
  43. reader = new BufferedReader (new InputStreamReader (in));
  44. String buf = null;
  45. While ((buf = Reader.readline ()) = null) {
  46. System.out.println (BUF);
  47. }
  48. }
  49. } catch (IOException e) {
  50. E.printstacktrace ();
  51. } catch (Jschexception e) {
  52. E.printstacktrace ();
  53. } finally {
  54. try {
  55. Reader.close ();
  56. } catch (IOException e) {
  57. E.printstacktrace ();
  58. }
  59. Channel.disconnect ();
  60. Session.disconnect ();
  61. }
  62. }
  63. }

Com.jcraft.jsch.ChannelSftp explained in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.