Advertisement
Guest User

Untitled

a guest
May 16th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 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("~/.ssh/known_hosts");
  15.                         java.util.Properties config = new java.util.Properties();
  16.                         config.put("StrictHostKeyChecking", "no");
  17.                         jschSession.setConfig(config);
  18.  
  19.                     } catch (JSchException e) {
  20.                         e.printStackTrace();
  21.                     }
  22.  
  23.                 } catch (Exception e) {
  24.                     Log.e("setupJsch", e.getMessage());
  25.                 }
  26.  
  27.             }
  28.         }).start();
  29.  
  30.         jschSession.setPassword(MainActivity._pass);
  31.         jschSession.connect();
  32.  
  33.         return (ChannelSftp) jschSession.openChannel("sftp");
  34.     }
  35.  
  36.     int requestcode = 1;
  37.     public void onActivityResult(int requestcode, int resultCode, Intent data){
  38.         super.onActivityResult(requestcode, resultCode, data);
  39.  
  40.         if(requestcode == requestcode && resultCode == Activity.RESULT_OK){
  41.             if(data == null){
  42.                 return;
  43.             }
  44.             Uri uri = data.getData();
  45.             Toast.makeText(ControlActivity.this, uri.getPath(), Toast.LENGTH_SHORT).show();
  46.  
  47.             uploadFile(Uri.parse(uri.getPath()).toString());
  48.         }
  49.     }
  50.  
  51.     public void openfilechooser(View view){
  52.  
  53.         //Toast.makeText(ControlActivity.this, "Feature not implemented yet", Toast.LENGTH_SHORT).show();
  54.  
  55.         //Throws error, not fixed, therefore "not implemented"
  56.         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  57.         intent.setType("*/*");
  58.         startActivityForResult(intent, requestcode);
  59.     }
  60.  
  61.     public void uploadFile(String path) {
  62.  
  63.         new Thread(new Runnable() {
  64.             public void run() {
  65.  
  66.                 ChannelSftp channelSftp = null;
  67.  
  68.                 try {
  69.                     channelSftp = setupJsch();
  70.                     channelSftp.connect();
  71.  
  72.                 } catch (JSchException e) {
  73.                     Log.e("SFTP: ",e.getMessage());
  74.                 }
  75.  
  76.  
  77.                 File file = new File(path);
  78.                 String localFile = file.getPath();
  79.                 String remoteDir = "/home/pi/Music/";
  80.  
  81.  
  82.                 try {
  83.                     channelSftp.put(localFile, remoteDir + file.getName());
  84.                 } catch (SftpException e) {
  85.                     Log.e("uploadFile-put", e.getMessage());
  86.                 }
  87.  
  88.                 channelSftp.exit();
  89.             }
  90.         }).start();
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement