android7.1使用sftp传输测试demo

源码: https://github.com/yhm2046/android_workspace/

项目流程:获取data/system/dropbox (立即上传) 和 data/logs/ (收到广播后12h上传)下所有文件打包成zip上传到服务器

平台系统:rk3288w,android7.1

代码清单:

\packages\apps\Settings\src\com\android\settings\HdmiReceiver.java (广播接收器)

\packages\apps\Settings\src\com\android\settings\LogActivity.java(处理data/system/dropbox 数据)

\packages\apps\Settings\src\com\android\settings\DataLogActivity.java(处理data/logs/ 数据)

\packages\apps\Settings\src\com\android\settings\EvanUtils.java(自定义工具类)

\packages\apps\Settings\src\com\android\settings\UtilsSftp.java(自定义工具类)

\packages\apps\Settings\src\com\android\settings\SFTPUtil.java(SFTP工具类)

\packages\apps\Settings\AndroidManifest.xml(加入静态广播)

判断是否联网

ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context
                    .CONNECTIVITY_SERVICE);
            // 获取NetworkInfo对象
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            if(networkInfo==null){
               Toast.makeText(context, "请先联网再上传log!!",Toast.LENGTH_SHORT).show();
            }

定义静态广播

<receiver android:name="HdmiReceiver">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.HDMI_PLUG"/>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <!--wp add android.intent.action.datalog.close-->
                <action android:name="android.intent.action.gmt.logupload" />
                <action android:name="android.intent.action.datalog.close" />
            </intent-filter>
       </receiver>
else if(action.equals(WP_TAG)){ //wp add 首先判断是否联网
        int switchResult=SystemProperties.getInt("persist.sys.abc_switch",0); //datalog的状态
              ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context
                    .CONNECTIVITY_SERVICE);
            // 获取NetworkInfo对象
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            if(networkInfo==null){
               Toast.makeText(context, "请先联网再上传log!!",Toast.LENGTH_SHORT).show();
            }
              else{
                if(switchResult == 1){//log 按钮已经打开
              Log.i(TAG,"wp log is open,don't open again-->"+switchResult);
              }else{//log没有打开
                   SystemProperties.set("persist.sys.abc_switch","1");
                   Log.i(TAG,"wp has opened,now status is"+SystemProperties.getInt("persist.sys.abc_switch",0));//打开按钮
                    //新开activity上传dropbx,抓取datalog,12h后停止,压缩上传,发action==timestop
                   Intent intActivity=new Intent(context,LogActivity.class);
                    intActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//must set this way!!
                    context.startActivity(intActivity);
                  } 
              }
       }else if(action.equals(DATA_LOG_TAG)){
            Log.i(TAG,"12小时已经到,关闭datalog开关,上传datalog到服务器");
            SystemProperties.set("persist.sys.abc_switch","0");
            try{
                    Intent intActivity=new Intent(context,DataLogActivity.class);
                    intActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//must set this way!!
                    context.startActivity(intActivity);
                    Log.i("wp----------------->","打开DataLogActivity成功!!!");
                  }catch (Exception e) {
                                          e.printStackTrace();
                                          Log.i("wp----------------->","打开DataLogActivity报错:"+e);
                                        }
       }

获取权限

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

LogActivity:

  1. 获取权限
  2. 用dropboxManager读取/data/system/dropbox 下的文件打包到到  /sdcard/dropboxlogs.zip
  3. dropboxlogs.zip上传到服务器 /evan/mac地址/dropboxlogs/
 /**
     * 檢查是否有權限
     * @param permission    權限名稱:String類型,Manifest.permission.READ_LOGS
     */
    private void getPermision(String permission) {
        int result2=ActivityCompat.checkSelfPermission(this, permission);
        if (result2 == PackageManager.PERMISSION_GRANTED)
        {
            // Log.i(Constants.TAG,"有权限");
        }
        else {
            // Log.i(Constants.TAG,"無权限,開始獲取");
            ActivityCompat.requestPermissions(this, new String[]{permission}, 1);
        }
        result2=ActivityCompat.checkSelfPermission(this, permission);
        Log.i(Constants.TAG,permission+"权限为--》"+result2);
    }
/**
     * 查询ip地址
     * @return  ip地址字符串
     */
    private static InetAddress getLocalInetAddress() {
        InetAddress ip = null;
        try {
            //  о
            Enumeration<NetworkInterface> en_netInterface = NetworkInterface
                    .getNetworkInterfaces();
            while (en_netInterface.hasMoreElements()) {//  Ƿ   Ԫ
                NetworkInterface ni = (NetworkInterface) en_netInterface
                        .nextElement();//  õ   һ  Ԫ
                Enumeration<InetAddress> en_ip = ni.getInetAddresses();//  õ һ  ip  ַ   о
                while (en_ip.hasMoreElements()) {
                    ip = en_ip.nextElement();
                    if (!ip.isLoopbackAddress()
                            && ip.getHostAddress().indexOf(":") == -1)
                        break;
                    else
                        ip = null;
                }
                Log.i(Constants.TAG,"ip-->"+ip);
                if (ip != null) {
                    break;
                }
            }
        } catch (SocketException e) {
            Log.i(Constants.TAG,"SocketException-->"+e);
            e.printStackTrace();
        }
        return ip;
    }

/**
     * 查询mac地址
     * @return  mac地址字符串
     */
    public static String getMacAddress() {
        String strMacAddr = null;
        try {
            //    IpD  ַ
            InetAddress ip = getLocalInetAddress();
            byte[] b = NetworkInterface.getByInetAddress(ip)
                    .getHardwareAddress();
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < b.length; i++) {
                if (i != 0) {
                    buffer.append(':');
                }
                String str = Integer.toHexString(b[i] & 0xFF);
                buffer.append(str.length() == 1 ? 0 + str : str);
            }
            strMacAddr = buffer.toString().toUpperCase();
            Log.i(Constants.TAG,"strMacAddr-->"+strMacAddr);
        } catch (Exception e) {
            Log.i(Constants.TAG,"exception-->"+e);
        }
        return strMacAddr;
    }
/**
     打包/data/system/dropbox/ 下面所有文件为zip格式,/sdcard/dropboxlog.zip
     String zipPath	压缩路径
     String zipName 压缩名称
     zipAllDropbox("/sdcard","dropboxlog")
     */
    public void zipAllDropbox(String zipPath,String zipName){
        // String sourceFilePath="/sdcard/dropboxlog";
        String sourceFilePath=zipPath+"/"+zipName;
        File sourceDir=new File(sourceFilePath);
        File zipFile=new File(sourceFilePath+".zip");
        ZipOutputStream zos=null;
        try {
            zos=new ZipOutputStream(new FileOutputStream(zipFile));
            // String baseDir="dropboxlog/";
            String baseDir=zipName+"/";
            EvanUtils.compressFiles(sourceDir,baseDir,zos);//遍历所有文件
            // Log.i(Constants.TAG,"打包壓縮主程序成功,路径为:"+sourceFilePath+".zip");
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(Constants.TAG,"打包壓縮主程序失敗--》"+e.toString());
        } finally {
            if(zos!=null)
                try {
                    zos.close();
                    Log.i(Constants.TAG,"打包壓縮主程序關閉zos成功---》");
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.i(Constants.TAG,"打包壓縮主程序關閉zos失敗--》"+e.toString());
                }
        }
    }
/**
     * 上传文件到服务器,evan/{mac address}/dropboxlog.zip
     filename  上传的文件名称
     filepath  上传的文件地址
     uploadDropbox("/sdcard","dropboxlog")
     */
    public void uploadDropbox(String filePath,String fileName){
        SFTPUtil sftp = new SFTPUtil("用户名", "密码", "服务器地址", 22);
        sftp.login();
        //作为压缩文件上传
        // File file = new File(Environment.getExternalStorageDirectory().getPath()+"/"+savaFileName);
        File file = new File(filePath+"/"+fileName+".zip");
        InputStream is = null;
        try {
            int length=(int)file.length();
            byte[] buff=new byte[length];
            FileInputStream fin=new FileInputStream(file);
            is = fin;
            String sftpPath="viomi/evan/"+macString.replace(':','_')
                    +"/";
            Log.i("wp------","upload fileName is***************************** "+fileName);        
            sftp.upload(sftpPath, fileName+".zip", is);
            Log.i("wp------","upload sucess,path is "+sftpPath);
        } catch (SftpException e) {
            Log.i("wp------SftpException",is.toString());
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.i("wp------FileNotFoundException",is.toString());
        }
        sftp.logout();
    }

SFTP工具类

package com.android.settings;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.Vector;
/**
 * 参考:https://blog.csdn.net/yhl_jxy/article/details/72633034
 * 工具类,上传sftp服务器
 */
public class SFTPUtil {
    private transient Logger log = LoggerFactory.getLogger(this.getClass());
    private ChannelSftp sftp;
    private Session session;
    /** FTP 登录用户名*/
    private String username;
    /** FTP 登录密码*/
    private String password;
    /** 私钥 */
    private String privateKey;
    /** FTP 服务器地址IP地址*/
    private String host;
    /** FTP 端口*/
    private int port;
    /**
     * 构造基于密码认证的sftp对象
     * @param username
     * @param password
     * @param host
     * @param port
     */
    public SFTPUtil(String username, String password, String host, int port) {
        this.username = username;
        this.password = password;
        this.host = host;
        this.port = port;
    }
    /**
     * 构造基于秘钥认证的sftp对象
     * @param username
     * @param host
     * @param port
     * @param privateKey
     */
    public SFTPUtil(String username, String host, int port, String privateKey) {
        this.username = username;
        this.host = host;
        this.port = port;
        this.privateKey = privateKey;
    }
    public SFTPUtil(){}

    /**
     * 連接服務器
     * @param host  ip地址
     * @param port  端口
     * @param username  用戶名
     * @param password  密碼
     * @return  ChannelSftp
     * @throws JSchException
     */
    public ChannelSftp connect(String host, int port, String username,String password) throws JSchException {
        // 1.声明连接Sftp的通道
        ChannelSftp nChannelSftp = null;
        // 2.实例化JSch
        JSch nJSch = new JSch();
        // 3.获取session
        Session nSShSession = nJSch.getSession(username, host, port);
        System.out.println("Session创建成功");
        // 4.设置密码
        nSShSession.setPassword(password);
        // 5.实例化Properties
        Properties nSSHConfig = new Properties();
        // 6.设置配置信息
        nSSHConfig.put("StrictHostKeyChecking", "no");
        // 7.session中设置配置信息
        nSShSession.setConfig(nSSHConfig);
        // 8.session连接
        nSShSession.connect();
        System.out.println("Session已连接");
        // 9.打开sftp通道
        Channel channel = nSShSession.openChannel("sftp");
        // 10.开始连接
        channel.connect();
        nChannelSftp = (ChannelSftp) channel;
        System.out.println("连接到主机" + host + ".");
        return nChannelSftp;
    }
    /**
     * 列出列表下的文件
     * @param directory 文件目錄
     * @param sftp  ChannelSftp
     * @return  Vector<String>文件名集合
     * @throws SftpException
     */
    public Vector listFiles(String directory, ChannelSftp sftp)throws SftpException {
        return sftp.ls(directory);
    }
    /**
     * 文件重命名
     * @param directory 文件目錄
     * @param oldname   原名稱
     * @param newname   新名稱
     * @param sftp  ChannelSftp
     */
    public void renameFile(String directory, String oldname, String newname,ChannelSftp sftp) {
        try {
            sftp.cd(directory);
            sftp.rename(oldname, newname);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 连接sftp服务器
     *
     * @throws Exception
     */
    public void login(){
        try {
            JSch jsch = new JSch();
            if (privateKey != null) {
                jsch.addIdentity(privateKey);// 设置私钥
                log.info("sftp connect,path of private key file:{}" , privateKey);
            }
            log.info("sftp connect by host:{} username:{}",host,username);
            session = jsch.getSession(username, host, port);
            log.info("Session is build");
            if (password != null) {
                session.setPassword(password);
            }
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            log.info("Session is connected");
            Channel channel = session.openChannel("sftp");
            channel.connect();
            log.info("channel is connected");
            sftp = (ChannelSftp) channel;
            log.info(String.format("wp--------->sftp server host:[%s] port:[%s] is connect successfull", host, port));
        } catch (JSchException e) {
            log.error("wp--------->Cannot connect to specified sftp server : {}:{} \n Exception message is: {}", new Object[]{host, port, e.getMessage()});
            log.info("wp---------make sure the net is connected!!");
        }
    }
    /**
     * 关闭连接 server
     */
    public void logout(){
        if (sftp != null) {
            if (sftp.isConnected()) {
                sftp.disconnect();
                log.info("sftp is closed already");
            }
        }
        if (session != null) {
            if (session.isConnected()) {
                session.disconnect();
                log.info("sshSession is closed already");
            }
        }
    }
    /**
     * 将输入流的数据上传到sftp作为文件,上传到文件夹,若无则新建一个
     *
     * @param directory
     *            上传到该目录
     * @param sftpFileName
     *            sftp端文件名
     * @param input
     *            输入流
     * @throws SftpException
     * @throws Exception
     */
    public void upload(String directory, String sftpFileName, InputStream input) throws SftpException {
        try {
            sftp.cd(directory);
        } catch (SftpException e) {
            log.warn("directory is not exist");
            sftp.mkdir(directory);
            sftp.cd(directory);
        }
        sftp.put(input, sftpFileName);
        log.info("file:{} is upload successful" , sftpFileName);
    }
    /**
     * 上传单个文件
     *
     * @param directory
     *            上传到sftp目录
     * @param uploadFile
     *            要上传的文件,包括路径
     * @throws FileNotFoundException
     * @throws SftpException
     * @throws Exception
     */
    public void upload(String directory, String uploadFile) throws FileNotFoundException, SftpException{
        File file = new File(uploadFile);
        upload(directory, file.getName(), new FileInputStream(file));
    }
    /**
     * 将byte[]上传到sftp,作为文件。注意:从String生成byte[]是,要指定字符集。
     *
     * @param directory
     *            上传到sftp目录
     * @param sftpFileName
     *            文件在sftp端的命名
     * @param byteArr
     *            要上传的字节数组
     * @throws SftpException
     * @throws Exception
     */
    public void upload(String directory, String sftpFileName, byte[] byteArr) throws SftpException{
        upload(directory, sftpFileName, new ByteArrayInputStream(byteArr));
    }
    /**
     * 将字符串按照指定的字符编码上传到sftp
     *
     * @param directory
     *            上传到sftp目录
     * @param sftpFileName
     *            文件在sftp端的命名
     * @param dataStr
     *            待上传的数据
     * @param charsetName
     *            sftp上的文件,按该字符编码保存
     * @throws UnsupportedEncodingException
     * @throws SftpException
     * @throws Exception
     */
    public void upload(String directory, String sftpFileName, String dataStr, String charsetName) throws UnsupportedEncodingException, SftpException{
        upload(directory, sftpFileName, new ByteArrayInputStream(dataStr.getBytes(charsetName)));
    }
    /**
     * 下载文件
     *
     * @param directory
     *            下载目录
     * @param downloadFile
     *            下载的文件
     * @param saveFile
     *            存在本地的路径
     * @throws SftpException
     * @throws FileNotFoundException
     * @throws Exception
     */
    public void download(String directory, String downloadFile, String saveFile) throws SftpException, FileNotFoundException{
        if (directory != null && !"".equals(directory)) {
            sftp.cd(directory);
        }
        File file = new File(saveFile);
        sftp.get(downloadFile, new FileOutputStream(file));
        log.info("file:{} is download successful" , downloadFile);
    }
    /**
     * 下载文件
     * @param directory 下载目录
     * @param downloadFile 下载的文件名
     * @return 字节数组
     * @throws SftpException
     * @throws IOException
     * @throws Exception
     */
    public byte[] download(String directory, String downloadFile) throws SftpException, IOException {
        if (directory != null && !"".equals(directory)) {
            sftp.cd(directory);
        }
        InputStream is = sftp.get(downloadFile);
        byte[] fileData = IOUtils.toByteArray(is);
        log.info("file:{} is download successful" , downloadFile);
        return fileData;
    }
    /**
     * 删除文件
     *
     * @param directory
     *            要删除文件所在目录
     * @param deleteFile
     *            要删除的文件
     * @throws SftpException
     * @throws Exception
     */
    public void delete(String directory, String deleteFile) throws SftpException{
        sftp.cd(directory);
        sftp.rm(deleteFile);
    }
    /**
     * 列出目录下的文件
     *
     * @param directory
     *            要列出的目录
     * @param directory
     * @return
     * @throws SftpException
     */
    public Vector<?> listFiles(String directory) throws SftpException {
        return sftp.ls(directory);
    }
    /**
     * 上传测试
     */
    public static void main(String[] args) throws SftpException, IOException {
    }
}

需要用到第三方jar包,拷贝到packages\apps\Settings\libs

链接:https://pan.baidu.com/s/1aOvcWPRo_1SlPQ5EZCcRlA 
提取码:bshv 
复制这段内容后打开百度网盘手机App,操作更方便哦

修改packages\apps\Settings\Android.mk

开启定时器触发DataActivity

Calendar calendar=Calendar.getInstance();
         calendar.add(Calendar.HOUR,12);    //12 hours 后執行
        Intent intent1=new Intent(DATA_LOG_TAG);
        AlarmManager alarmManager=(AlarmManager)getSystemService(this.ALARM_SERVICE);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(this,1,intent1,0);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
        Log.i("wp------->","计时器已经启动!!!!!!!!!!!!!!!!!!!");

DataActivity:

/data/logs 下的数据没有类似DropboxManager的方法读取,自己写一个方法拷贝到sdcard再上传,其它流程方法跟LoginActivity一样

/**
     * 复制一个目录及其子目录、文件到另外一个目录
     * @param src
     * @param dest
     * @throws IOException
     */
    private void copyFolder(File src, File dest) throws IOException {
        if (src.isDirectory()) {
            if (!dest.exists()) {
                dest.mkdir();
            }
            String files[] = src.list();
            for (String file : files) {
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                // 递归复制
                copyFolder(srcFile, destFile);
            }
        } else {
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            in.close();
            out.close();
        }
    }

代码以上,水平有限,欢迎指教。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值