在Android上的Jsch rm线程同步

时间:2012-12-27 12:19:24

标签: android multithreading ssh jsch

我正在使用android开发sftp。我使用JSch在服务器和设备之间建立通道,但我遇到了问题:

首先,用户必须选择要下载的文件列表,并为每个文件创建intentService以下载文件。然后我做一个md5检查以确定下载是否正常,最后如果完整性正常,我必须从服务器中删除该文件。

主要问题是我有一个错误:SftpException 4:没有描述,我认为当我尝试从服务器删除文件时服务已关闭,因此关闭了通道并且找不到文件。

我有其他活动监控所有下载+全球进展。但我无法删除此文件,因为MainThread上的网络...所以我想我可以在一个单独的线程上创建SftpChannel并将其提供给活动。但是我怎么能这样做呢?我知道BroadcastIntents无法发送这些类型的数据......

所以要恢复:

DownloadSFTPService => IntentService : open the SftpChannel并下载该文件,将数据发送到RunningDownloadsActivity以进行升级。

RunningDownloadsActivity => ListActivity : Monitor all the downloads + global receiving via BrodcastIntent updates from DownloadSftpService. 

DownloadSftpService:

protected void onHandleIntent(Intent intent) {

    //initializations

    //Création de la session avec le serveur
    JSch jsch = new JSch();
    Session session = null;
    try {
        session = jsch.getSession(loginFtp, ipServer, GlobalEnum.DOWNLOAD.PORT);
        if(session == null)
        {
            Log.e("SSHError","Impossible d'instancier la session");
        }
        else
        {

            //Configuration de la connexion par mot de passe plutot que certificat
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            config.put("PreferredAuthentifications", "password");
            session.setConfig(config);

            //Récupération du mot de passe dans les préférences et ouverture de session avec le serveur
            session.setPassword(passwordFtp);
            session.connect();

            //Configuration du canal en mode sFTP et ouverture
            Channel channel = session.openChannel("sftp");
            sftpChannel = (ChannelSftp) channel;
            sftpChannel.connect();

            //Initialisation de la notification
            initialiseNotif();

            //Lancement du téléchargement des fichiers
            downloadFiles();

            //Clôture de la connexion
            sftpChannel.disconnect();

        }
    } catch (JSchException e) {
        Log.e("JschException", e.toString());
    } catch (SftpException e) {
        Log.e("SftpException", e.toString());
    }

}

    private void downloadFiles() throws SftpException
{
    Vector<ChannelSftp.LsEntry> file;

    //Pour chaque fichier
    for( String tempFileName : downloadList)
    {           
        if(fileExisting(tempFileName)) {
            //Récupération des infos fichier
            file = sftpChannel.ls(tempFileName);
            currentFileSize = file.get(0).getAttrs().getSize();
            currentFileName = file.get(0).getFilename();

            broadcastIntentRD.putExtra(GlobalEnum.DOWNLOAD.FILE_NAME, currentFileName);
            broadcastIntentRD.putExtra(GlobalEnum.DOWNLOAD.FILE_SIZE, currentFileSize);

            //Lancement du téléchargement 

            int lastDot = tempFileName.lastIndexOf(".");
            String baseFileName = null;
            if(tempFileName.endsWith("tar.gz"))
            {
                String temp = tempFileName.substring(0, lastDot);
                baseFileName = temp.substring(0, temp.lastIndexOf("."));
            } else {
                baseFileName = tempFileName.substring(0, lastDot );
            }

            try {
                sftpChannel.get(baseFileName + ".md5", Environment.getExternalStorageDirectory() + vamsillaPath + "/" + baseFileName + ".md5");
            } catch (SftpException e) {
                Log.v("Sftp", "md5 introuvable pour : " + currentFileName);
            }
            sftpChannel.get(file.get(0).getFilename(), Environment.getExternalStorageDirectory() + vamsillaPath + "/" + file.get(0).getFilename(),this);
        }

    }

RunningDownloadsActivity:

public class DownloadReceiverRD extends BroadcastReceiver{


    @Override
    public void onReceive(Context context, Intent intent) {

        //BLABLA for notification and progressbar update

                    if(progress == fileSize){

                        //BLABLA for notification and progressbar closing


                        md5Sum(fileName);
                    }
                }
      private void md5Sum(String currentFileName) {
        VamsillaTools tools = new VamsillaTools(getApplicationContext());

        //Ouverture de la base de stockage des fichiers

        //Insertion du résultat dans la BDD
        int result = tools.checkMD5(currentFileName);
        db.insertFile(currentFileName, result);
        switch(result) {
            case -1 : Log.v("MD5", "Problème lors de la comparaison md5 : " + currentFileName); break;
            case 0 : Log.v("MD5", "Comparaison md5 fausse : " + currentFileName); break;
            case 1 : Log.v("MD5", "Succès de la comparaison md5 pour : " + currentFileName);

    /********
      SUPRESS THE FILE HERE WOULD BE PERFECT !!! 
    *********/

   break;
        }
    }
}      

请原谅我的英语,我是法国人,你知道我们对外语有多好......

谢谢!

1 个答案:

答案 0 :(得分:0)

可能会迟到,但如果可以帮助某人......  我终于juste做了一个继承自Thread的类做网络+删除文件,当我们停止搜索困难的方法时,它很简单:)

相关问题