在Android 中實現scp操作


本文簡單介紹用SSHganymed-ssh2Android中實現scp操作。

SSH

SSH是專為遠程登錄會話和其他網絡服務提供安全性的協議,簡單的說就是一種網絡協議。是linux的標准配置。用於linux設備之間的通訊。

SCP

SCP是一種基於SSH完成加密拷貝文件的協議。使用SSH進行身份認證確保數據傳輸的真實性和可靠性。

SCP默認通過TCP端口22運行

SCP程序常用語法:


// 復制文件到主機
scp SourceFile user@host:directory/TargetFile


// 從主機復制文件
scp user@host:directory/SourceFile TargetFile
scp -r user@host:directory/SourceFolder TargetFolder

SFTP

SFTP也是基於SSH安全文件傳輸協議。不同於基於FTP,FTP基於Tcp使用明文傳輸用戶信息。安全性較差。

Android中使用SCP

  • 下載ganymed-ssh2 jar包

<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
<dependency>
    <groupId>ch.ethz.ganymed</groupId>
    <artifactId>ganymed-ssh2</artifactId>
    <version>build210</version>
</dependency>


官方下載地址 http://www.ganymed.ethz.ch/ssh2/


public class Scp {

    private volatile static Scp scpInstance;

    private String user;
    private String pass;
    private String host;
    private Connection connection;
    private SCPClient scpClient;
    private Boolean isAuthed;

    private Scp(String user, String pass, String host){
        this.user = user;
        this.pass = pass;
        this.host = host;
    }

    public static Scp getScpUtilsInstance(String user, String pass, String host){

        if(scpInstance == null) {
            synchronized(Scp.class) {
                if(scpInstance == null) {
                    scpInstance = new Scp(user,pass,host);
                }
            }
        }
        return scpInstance;
    }


    public void connect(){
        connection = new Connection(host);
        try {
            connection.connect();
            isAuthed = connection.authenticateWithPassword(user,pass);
            // scp 連接
            scpClient = connection.createSCPClient();
        } catch (IOException e) {
            e.printStackTrace();
            close();
        }
    }

    public void close(){
        connection.close();
        sftPv3Client.close();
    }

    public boolean getIsAuthed(){
        return isAuthed;
    }

    // 拷貝文件到服務器
    public void putFile(String filePath,String aimPath){
        try {
            if(scpClient != null){
                scpClient.put(filePath,aimPath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  

}


Scp scp = Scp.getScpUtilsInstance("root","psd","192.168.199.3");
                            scp.connect();
                            if(scp.getIsAuthed()){
                                for(int i = 0;i<data.getLayers();i++){
                                    scp.putFile(SlcParser.pngDirectory+"/"+i+".png","/home");
                                }
                            }


scp

SFTP 刪除文件



    private SFTPv3Client sftPv3Client;
    
   sftPv3Client = new SFTPv3Client(connection);

    public void rmFile(String filePath){
            try {
                if(sftPv3Client != null){
                    sftPv3Client.rm(filePath);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


Scp scp = Scp.getScpUtilsInstance("root","psd","192.168.199.3");
                scp.connect();
                if(scp.getIsAuthed()){
                    for(int i = 0;i<10;i++){
                        scp.rmFile("/home/"+i+".png");
                    }
                }


scp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM