Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private ChannelSftp setupJsch() throws JSchException {
- JSch jsch = new JSch();
- Session jschSession = jsch.getSession(MainActivity._user, MainActivity._host);
- new Thread(new Runnable() {
- public void run() {
- try {
- try {
- jsch.setKnownHosts("~/.ssh/known_hosts");
- java.util.Properties config = new java.util.Properties();
- config.put("StrictHostKeyChecking", "no");
- jschSession.setConfig(config);
- } catch (JSchException e) {
- e.printStackTrace();
- }
- } catch (Exception e) {
- Log.e("setupJsch", e.getMessage());
- }
- }
- }).start();
- jschSession.setPassword(MainActivity._pass);
- jschSession.connect();
- return (ChannelSftp) jschSession.openChannel("sftp");
- }
- int requestcode = 1;
- public void onActivityResult(int requestcode, int resultCode, Intent data){
- super.onActivityResult(requestcode, resultCode, data);
- if(requestcode == requestcode && resultCode == Activity.RESULT_OK){
- if(data == null){
- return;
- }
- Uri uri = data.getData();
- Toast.makeText(ControlActivity.this, uri.getPath(), Toast.LENGTH_SHORT).show();
- uploadFile(Uri.parse(uri.getPath()).toString());
- }
- }
- public void openfilechooser(View view){
- //Toast.makeText(ControlActivity.this, "Feature not implemented yet", Toast.LENGTH_SHORT).show();
- //Throws error, not fixed, therefore "not implemented"
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.setType("*/*");
- startActivityForResult(intent, requestcode);
- }
- public void uploadFile(String path) {
- new Thread(new Runnable() {
- public void run() {
- ChannelSftp channelSftp = null;
- try {
- channelSftp = setupJsch();
- channelSftp.connect();
- } catch (JSchException e) {
- Log.e("SFTP: ",e.getMessage());
- }
- File file = new File(path);
- String localFile = file.getPath();
- String remoteDir = "/home/pi/Music/";
- try {
- channelSftp.put(localFile, remoteDir + file.getName());
- } catch (SftpException e) {
- Log.e("uploadFile-put", e.getMessage());
- }
- channelSftp.exit();
- }
- }).start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement