Advertisement
Guest User

Untitled

a guest
May 15th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. private ChannelSftp setupJsch() throws JSchException {
  2.  
  3.         JSch jsch = new JSch();
  4.         Session jschSession = jsch.getSession(MainActivity._user, MainActivity._host);
  5.  
  6.         new Thread(new Runnable() {
  7.             public void run() {
  8.  
  9.  
  10.                 try {
  11.  
  12.  
  13.                     try {
  14.                         jsch.setKnownHosts("/Users/john/.ssh/known_hosts");
  15.                     } catch (JSchException e) {
  16.                         e.printStackTrace();
  17.                     }
  18.  
  19.  
  20.                     jschSession.setPassword(MainActivity._pass);
  21.                     jschSession.connect();
  22.  
  23.  
  24.                 } catch (Exception e) {
  25.                     e.printStackTrace();
  26.                 }
  27.  
  28.             }
  29.         }).start();
  30.  
  31.         return (ChannelSftp) jschSession.openChannel("sftp");
  32.     }
  33.  
  34.     int requestcode = 1;
  35.     public void onActivityResult(int requestcode, int resultCode, Intent data){
  36.         super.onActivityResult(requestcode, resultCode, data);
  37.  
  38.         if(requestcode == requestcode && resultCode == Activity.RESULT_OK){
  39.             if(data == null){
  40.                 return;
  41.             }
  42.             Uri uri = data.getData();
  43.             Toast.makeText(ControlActivity.this, uri.getPath(), Toast.LENGTH_SHORT).show();
  44.  
  45.             uploadFile(uri.getPath());
  46.         }
  47.     }
  48.  
  49.     public void openfilechooser(View view){
  50.         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  51.         intent.setType("*/*");
  52.         startActivityForResult(intent, requestcode);
  53.     }
  54.  
  55.     public void uploadFile(String path) {
  56.  
  57.  
  58.         new Thread(new Runnable() {
  59.             public void run() {
  60.  
  61.                 ChannelSftp channelSftp = null;
  62.  
  63.                 try {
  64.                     channelSftp = setupJsch();
  65.                     channelSftp.connect();
  66.  
  67.                 } catch (JSchException e) {
  68.                     Log.e("SFTP: ",e.getMessage());
  69.                 }
  70.  
  71.  
  72.                 String localFile = path;
  73.                 String remoteDir = "/home/pi/Music";
  74.  
  75.                 try {
  76.                     channelSftp.put(localFile, remoteDir + "jschFile.txt");
  77.                 } catch (SftpException e) {
  78.                     e.printStackTrace();
  79.                 }
  80.  
  81.                 channelSftp.exit();
  82.             }
  83.         }).start();
  84.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement