Advertisement
Guest User

Untitled

a guest
Dec 28th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. import com.jcraft.jsch.Channel;
  2. import com.jcraft.jsch.ChannelSftp;
  3. import com.jcraft.jsch.JSch;
  4. import com.jcraft.jsch.JSchException;
  5. import com.jcraft.jsch.Session;
  6. import com.jcraft.jsch.SftpException;
  7. import java.io.BufferedInputStream;
  8. import java.io.BufferedOutputStream;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13.  
  14. /**
  15. *
  16. * @author it.i88.ca
  17. */
  18. public class NewClass {
  19.  
  20. public static void main(String[] args) {
  21.  
  22. String SFTPHOST = "192.168.3.70";
  23. int SFTPPORT = 22;
  24. String SFTPUSER = "user";
  25. String SFTPPASS = "passwd";
  26. String SFTPWORKINGDIR = "test";
  27. Session session = null;
  28. Channel channel = null;
  29. ChannelSftp channelSftp = null;
  30. try {
  31. JSch jsch = new JSch();
  32. session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
  33. session.setPassword(SFTPPASS);
  34. java.util.Properties config = new java.util.Properties();
  35. config.put("StrictHostKeyChecking", "no");
  36. session.setConfig(config);
  37. int timeOutMilliseconds = 60000;
  38. session.connect(timeOutMilliseconds);
  39.  
  40. channel = session.openChannel("sftp");
  41. channel.connect();
  42. channelSftp = (ChannelSftp) channel;
  43. channelSftp.cd(SFTPWORKINGDIR);
  44.  
  45. byte[] buffer = new byte[1024];
  46. BufferedInputStream bis = new BufferedInputStream(channelSftp.get("test.txt"));
  47. File newFile = new File("test.i88.ca.txt");
  48. OutputStream os = new FileOutputStream(newFile);
  49. BufferedOutputStream bos = new BufferedOutputStream(os);
  50. int readCount;
  51. while ((readCount = bis.read(buffer)) > 0) {
  52. bos.write(buffer, 0, readCount);
  53. }
  54. bis.close();
  55. bos.close();
  56. channel.disconnect();
  57. session.disconnect();
  58. //return;
  59. } catch (JSchException | SftpException | IOException ex) {
  60. ex.printStackTrace();
  61. }
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement