Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.OutputStream;
  6.  
  7. import com.jcraft.jsch.Channel;
  8. import com.jcraft.jsch.ChannelSftp;
  9. import com.jcraft.jsch.JSch;
  10. import com.jcraft.jsch.Session;
  11.  
  12. /**
  13. * @author https://kodehelp.com
  14. *
  15. */
  16. public class SFTPinJava {
  17.  
  18. /**
  19. * @param args
  20. */
  21. public static void main(String[] args) {
  22. String SFTPHOST = "10.20.30.40";
  23. int SFTPPORT = 22;
  24. String SFTPUSER = "username";
  25. String SFTPPASS = "password";
  26. String SFTPWORKINGDIR = "/export/home/kodehelp/";
  27.  
  28. Session session = null;
  29. Channel channel = null;
  30. ChannelSftp channelSftp = null;
  31.  
  32. try {
  33. JSch jsch = new JSch();
  34. session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
  35. session.setPassword(SFTPPASS);
  36. java.util.Properties config = new java.util.Properties();
  37. config.put("StrictHostKeyChecking", "no");
  38. session.setConfig(config);
  39. session.connect();
  40. channel = session.openChannel("sftp");
  41. channel.connect();
  42. channelSftp = (ChannelSftp) channel;
  43. channelSftp.cd(SFTPWORKINGDIR);
  44. File f = new File(FILETOTRANSFER);
  45. channelSftp.put(new FileInputStream(f), f.getName());
  46. } catch (Exception ex) {
  47. ex.printStackTrace();
  48. }
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement