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("/Users/john/.ssh/known_hosts");
- } catch (JSchException e) {
- e.printStackTrace();
- }
- jschSession.setPassword(MainActivity._pass);
- jschSession.connect();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }).start();
- 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.getPath());
- }
- }
- public void openfilechooser(View view){
- 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());
- }
- String localFile = path;
- String remoteDir = "/home/pi/Music";
- try {
- channelSftp.put(localFile, remoteDir + "jschFile.txt");
- } catch (SftpException e) {
- e.printStackTrace();
- }
- channelSftp.exit();
- }
- }).start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement