Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android STFP library [closed]

Tags:

android

sftp

I want to use SFTP in my Android project. Does Android already have an SFTP library, or do I have to implement it?

like image 993
Mr. Kakakuwa Bird Avatar asked Feb 12 '10 10:02

Mr. Kakakuwa Bird


3 Answers

I use andFTP for sftp transfers but it's not open source.

You can check connectBot. There is an issue about sftp transfers.

like image 95
Macarse Avatar answered Nov 15 '22 19:11

Macarse


Yes, edtFTPj/PRO is a commercial Java library that works on Android and supports SFTP (as well as FTP and FTPS).

like image 44
Bruce Blackshaw Avatar answered Nov 15 '22 19:11

Bruce Blackshaw


You can use jsch.

Gradle:

compile group: 'com.jcraft', name: 'jsch', version: '0.1.54'

Proguard (I do keep it public and ignore warnings. Easy solution, overkill. I choose not to mess with it here). If you know correct solution - let me know.

-keep class com.jcraft.jsch.jce.*
-keep class * extends com.jcraft.jsch.KeyExchange
-keep class com.jcraft.jsch.**
-keep class com.jcraft.jzlib.**
-keep class com.jcraft.jsch.jce.*
-keep class com.jcraft.jzlib.ZStream
-keep class com.jcraft.jsch.Compression
-keep class org.ietf.jgss.*
-dontwarn org.ietf.jgss.**
-dontwarn com.jcraft.jsch.**

code:

// add correct exception-handling; remember to close connection in all cases
public void doUpload(String host, String user, String password, String folder, int port, File file){
    JSch jsch = new JSch();

    Session session = jsch.getSession(user, host, port);
    session.setPassword(password);

    java.util.Properties config = new java.util.Properties();
    //Don't do it on Production -- makes it MITM-vulnerable
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.setTimeout(5000);
    session.setConfig("PreferredAuthentications", "password");
    session.connect();

    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp channelSftp = (ChannelSftp) channel;

    String home = channelSftp.getHome();
    if (folder == null || folder.length() == 0 || "/".equals(folder)) {
        folder = home;
    } else {
        File file = new File(new File(home), folder);
        folder = file.getPath();
    }
    channelSftp.cd(folder);

    try (BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(file.getPath()))) {
        ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressListener, file.length());
        channelSftp.put(progressInput, file.getName());
    }

    channelSftp.disconnect();
    session.disconnect();
}
like image 33
babay Avatar answered Nov 15 '22 17:11

babay