移动大脑-SpringMVc搭建RestFul后台服务(七)- 增量更新

477 阅读20分钟

目录:

        移动大脑-SpringMVc搭建RestFul后台服务(一)-环境搭建

        移动大脑-SpringMVc搭建RestFul后台服务(二)-配置mysql数据库

        移动大脑-SpringMVc搭建RestFul后台服务(三)-RestFul接口编写(模拟用户注册登录)

        移动大脑-SpringMVc搭建RestFul后台服务(四)-添加Token过滤器

        移动大脑-SpringMVc搭建RestFul后台服务(五)-支付宝支付

         移动大脑-SpringMVc搭建RestFul后台服务(六)-微信支付(Android)

        移动大脑-SpringMVc搭建RestFul后台服务(七)-增量更新

        前段时间抽空把增量更新给写好了,增量更新博客见《Android增量更新(一)-差分文件(Windows-part1)》。今天这篇博客就是增量更新的实际应用。先看看效果,由于增量更新是第一次访问时才根据客户端版本生成的差分文件并保存的,所以第一次访问会稍微慢点,后面访问就很快了:

首次访问:


以后访问:


看这效果还是不错的~。


一、首先讲讲大体业务逻辑:

1、当客户端(android)检查更新时会把当前apk的md5值,版本号,渠道号等信息发送给服务器;

2、服务器收到客户端的版本信息,首先根据md5值、版本号和渠道号在数据库中查找是否生成过相应的差分包,有记录就直接返回差分包下载地址;没有记录就循环对比和客户端apk的md5值相等的apk包,这个包就是旧apk,然后和渠道一致的新版本的apk包生成差分包,相应的查分表保存到差分文件夹下并存储差分信息到数据库并返回给客户端。如果没有想要渠道的新包,就表示没有更新。


二、创建更新实体类和配置数据表

2.1、更新实体类UpdateBean.java(get和set方法自行添加)

[java] view plain copy print?
  1. package com.ywl5320.appservice.bean;  
  2.   
  3. /** 
  4.  * Created by ywl5320 on 2017/10/26. 
  5.  */  
  6. public class UpdateBean extends BaseBean{  
  7.   
  8.     private Integer id;  
  9.     /** 
  10.      * old apk md5 值 
  11.      */  
  12.     private String md5value;  
  13.   
  14.     /** 
  15.      * 旧版本号(code) 
  16.      */  
  17.     private Integer versioncode;  
  18.   
  19.     /** 
  20.      * 旧版本号(name) 
  21.      */  
  22.     private String versionName;  
  23.   
  24.     /** 
  25.      * 新版本号(code) 
  26.      */  
  27.     private Integer newversioncode;  
  28.   
  29.     /** 
  30.      * 新版本号(name) 
  31.      */  
  32.     private String newversionName;  
  33.   
  34.     /** 
  35.      * 文件总大小 
  36.      */  
  37.     private Long filesize;  
  38.   
  39.     /** 
  40.      * patch包大小 
  41.      */  
  42.     private Long patchsize;  
  43.   
  44.     /** 
  45.      * 下载地址 
  46.      */  
  47.     private String downloadpath;  
  48.   
  49.     /** 
  50.      * 差分包下载地址 
  51.      */  
  52.     private String patchdownloadpath;  
  53.   
  54.     /** 
  55.      * 渠道标识 
  56.      */  
  57.     private String channelid;  
  58.   
  59.       
  60. }  
package com.ywl5320.appservice.bean;

/**
 * Created by ywl5320 on 2017/10/26.
 */
public class UpdateBean extends BaseBean{

    private Integer id;
    /**
     * old apk md5 值
     */
    private String md5value;

    /**
     * 旧版本号(code)
     */
    private Integer versioncode;

    /**
     * 旧版本号(name)
     */
    private String versionName;

    /**
     * 新版本号(code)
     */
    private Integer newversioncode;

    /**
     * 新版本号(name)
     */
    private String newversionName;

    /**
     * 文件总大小
     */
    private Long filesize;

    /**
     * patch包大小
     */
    private Long patchsize;

    /**
     * 下载地址
     */
    private String downloadpath;

    /**
     * 差分包下载地址
     */
    private String patchdownloadpath;

    /**
     * 渠道标识
     */
    private String channelid;

    
}


2.2、在entitis.hbm.xml中配置更新数据表t_update [html] view plain copy print?
  1. <class name="com.ywl5320.appservice.bean.UpdateBean"  table="t_update">  
  2.         <id name="id"  column="id">  
  3.             <generator class= "native"/>  
  4.         </id>  
  5.         <property name="md5value"  column="md5value"></property>  
  6.         <property name="versioncode"  column="versioncode"></property>  
  7.         <property name="versionName"  column="versionName"></property>  
  8.         <property name="newversioncode"  column="newversioncode"></property>  
  9.         <property name="newversionName"  column="newversionName"></property>  
  10.         <property name="filesize"  column="filesize"></property>  
  11.         <property name="patchsize"  column="patchsize"></property>  
  12.         <property name="downloadpath"  column="downloadpath"></property>  
  13.         <property name="patchdownloadpath"  column="patchdownloadpath"></property>  
  14.         <property name="channelid"  column="channelid"></property>  
  15.     </class>  
<class name="com.ywl5320.appservice.bean.UpdateBean" table="t_update">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="md5value" column="md5value"></property>
        <property name="versioncode" column="versioncode"></property>
        <property name="versionName" column="versionName"></property>
        <property name="newversioncode" column="newversioncode"></property>
        <property name="newversionName" column="newversionName"></property>
        <property name="filesize" column="filesize"></property>
        <property name="patchsize" column="patchsize"></property>
        <property name="downloadpath" column="downloadpath"></property>
        <property name="patchdownloadpath" column="patchdownloadpath"></property>
        <property name="channelid" column="channelid"></property>
    </class>


三、创建更新dao层

3.1、UpdateDao.java接口提供更新、存储和删除更新记录的功能

[java] view plain copy print?
  1. package com.ywl5320.appservice.dao;  
  2.   
  3. import com.ywl5320.appservice.bean.UpdateBean;  
  4.   
  5. /** 
  6.  * Created by ywl5320 on 2017/10/27. 
  7.  */  
  8. public interface UpdateDao {  
  9.   
  10.     /** 
  11.      * 获取更新信息 
  12.      * @param md5value 
  13.      * @param versioncode 
  14.      * @return 
  15.      */  
  16.     UpdateBean getUpdateInfo(String md5value, int versioncode, String channelid);  
  17.   
  18.     /** 
  19.      * 存储更新信息 
  20.      * @param updateBean 
  21.      */  
  22.     void saveUpdateInfo(UpdateBean updateBean);  
  23.   
  24.     /** 
  25.      * 删除更新信息 
  26.      * @param updateBean 
  27.      */  
  28.     void deleteUpdateInfo(UpdateBean updateBean);  
  29.   
  30. }  
package com.ywl5320.appservice.dao;

import com.ywl5320.appservice.bean.UpdateBean;

/**
 * Created by ywl5320 on 2017/10/27.
 */
public interface UpdateDao {

    /**
     * 获取更新信息
     * @param md5value
     * @param versioncode
     * @return
     */
    UpdateBean getUpdateInfo(String md5value, int versioncode, String channelid);

    /**
     * 存储更新信息
     * @param updateBean
     */
    void saveUpdateInfo(UpdateBean updateBean);

    /**
     * 删除更新信息
     * @param updateBean
     */
    void deleteUpdateInfo(UpdateBean updateBean);

}


3.1、实现dao接口
[java] view plain copy print?
  1. package com.ywl5320.appservice.dao;  
  2.   
  3. import com.ywl5320.appservice.bean.UpdateBean;  
  4. import org.springframework.orm.hibernate5.support.HibernateDaoSupport;  
  5.   
  6. import java.util.List;  
  7.   
  8. /** 
  9.  * Created by hlwky001 on 2017/10/27. 
  10.  */  
  11. public class UpdateDaoImpl extends HibernateDaoSupport  implements UpdateDao {  
  12.   
  13.     public UpdateBean getUpdateInfo(String md5value, int versioncode, String channelid) {  
  14.         List<UpdateBean> updates = (List<UpdateBean>) this.getHibernateTemplate().find("from UpdateBean where md5value=? and versioncode=? and channelid=?", md5value, versioncode, channelid);  
  15.         if(updates != null && updates.size() >  0)  
  16.         {  
  17.             return updates.get(0);  
  18.         }  
  19.         return null;  
  20.     }  
  21.   
  22.     public void saveUpdateInfo(UpdateBean updateBean) {  
  23.         this.getHibernateTemplate().save(updateBean);  
  24.     }  
  25.   
  26.     public void deleteUpdateInfo(UpdateBean updateBean) {  
  27.         this.getHibernateTemplate().delete(updateBean);  
  28.     }  
  29. }  
package com.ywl5320.appservice.dao;

import com.ywl5320.appservice.bean.UpdateBean;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by hlwky001 on 2017/10/27.
 */
public class UpdateDaoImpl extends HibernateDaoSupport implements UpdateDao {

    public UpdateBean getUpdateInfo(String md5value, int versioncode, String channelid) {
        List<UpdateBean> updates = (List<UpdateBean>) this.getHibernateTemplate().find("from UpdateBean where md5value=? and versioncode=? and channelid=?", md5value, versioncode, channelid);
        if(updates != null && updates.size() > 0)
        {
            return updates.get(0);
        }
        return null;
    }

    public void saveUpdateInfo(UpdateBean updateBean) {
        this.getHibernateTemplate().save(updateBean);
    }

    public void deleteUpdateInfo(UpdateBean updateBean) {
        this.getHibernateTemplate().delete(updateBean);
    }
}


四、创建service层,这也是重点

4.1、更新逻辑处理

1、定义apk文件命名规则:apkname_versioncode_versionname_channelid.apk。如:app_1_v1.0.0_xiaomi.apk。这样就能根据名字取出apk的版本信息和渠道信息。

2、创建文件夹oldversion存储旧版本的apk;创建文件夹newversion存储新版本的apk;创建文件夹patch存储旧apk和新apk生成的差分包。

3、当获取到客户端的apk信息,然后遍历oldversion文件夹更加md5值找出和客户端一致的apk;然后根据客户端apk的渠道id和版本号在newversion文件夹中找出是否有相应的更新包:如果有就生成差分包,返回差分包的下载地址并存储差分包对应的版本信息;没有就返回“已是最新版本”给客户端。


4.2、首先添加增量更新库,在博客《Android增量更新(二)-差分文件(Windows-part2)-dll动态库和jar包》和《Android增量更新(三)-差分文件(Linux)-生成jar和.so库》中生成的dll或so库和jar包,如:


相应的动态库需要配置到环境变量里面: Windows: dll库位置:D:\DevelopSofts\dll\BsDiffYwl5320.dll 然后把路径添加到系统环境变量D:\DevelopSofts\dll\  如图:


Linux:

so库位置:/usr/dev/mylib/BsDiffYwl5320.so

添加到环境变量:

[plain] view plain copy print?
  1. vim ~/.bashrc    
  2. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/dev/mylib    
  3. source ~/.bashrc    
vim ~/.bashrc  
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/dev/mylib  
source ~/.bashrc  
这样就jar包就可以调用dll和so库了。


4.2、生成patch包(ps:这是我的逻辑,还可以有更好的)

UpdateService.java

[java] view plain copy print?
  1. package com.ywl5320.appservice.service;  
  2.   
  3. import com.ywl5320.appservice.bean.RestFulBean;  
  4. import com.ywl5320.appservice.bean.UpdateBean;  
  5. import com.ywl5320.appservice.dao.UpdateDao;  
  6. import com.ywl5320.appservice.util.CommonUtils;  
  7. import com.ywl5320.appservice.util.RestFulUtil;  
  8. import com.ywl5320.bsdiff.BsDiffYwl5320Util;  
  9. import org.springframework.beans.factory.annotation.Autowired;  
  10. import org.springframework.transaction.annotation.Transactional;  
  11.   
  12. import java.io.File;  
  13.   
  14. /** 
  15.  * Created by ywl5320 on 2017/10/26. 
  16.  */  
  17. @Transactional  
  18. public class UpdateService {  
  19.   
  20.     @Autowired  
  21.     private UpdateDao updateDao;  
  22.   
  23.     static String oldApksPath = "E:/source/oldversion";  
  24.     static String newApkPath = "E:/source/newversion";  
  25.     static String patchPath = "E:/source/patch";  
  26.   
  27.     static  
  28.     {  
  29.         if(CommonUtils.getOsName().contains("linux"))//linux操作系统  
  30.         {  
  31.             oldApksPath = "/usr/dev/sources/oldversion";  
  32.             newApkPath = "/usr/dev/sources/newversion";  
  33.             patchPath = "/usr/dev/sources/patch";  
  34.         }  
  35.         else if(CommonUtils.getOsName().contains("windows")) //window操作系统  
  36.         {  
  37.             oldApksPath = "E:/source/oldversion";  
  38.             newApkPath = "E:/source/newversion";  
  39.             patchPath = "E:/source/patch";  
  40.         }  
  41.     }  
  42.   
  43.     public RestFulBean<UpdateBean> checkUpdate(String md5value, int versioncode, String channelid){  
  44.   
  45.         UpdateBean updateBean = updateDao.getUpdateInfo(md5value, versioncode, channelid);  
  46.         if(updateBean != null)  
  47.         {  
  48.             File file = new File(patchPath + "/" + updateBean.getPatchdownloadpath());  
  49.             if(file.exists()) {  
  50.                 return RestFulUtil.getInstance().getResuFulBean(updateBean,  0, "有新版本");  
  51.             }  
  52.             //todo 不存在可以再生产增量包或删除此条记录  
  53.             updateDao.deleteUpdateInfo(updateBean);  
  54.             return RestFulUtil.getInstance().getResuFulBean(null,  1, "没有新版本");  
  55.         }  
  56.         else  
  57.         {  
  58.             updateBean = createPatch(md5value, versioncode, channelid);  
  59.             if(updateBean != null)  
  60.             {  
  61.                 updateDao.saveUpdateInfo(updateBean);  
  62.                 UpdateBean updateBean1 = updateDao.getUpdateInfo(md5value, versioncode, channelid);  
  63.                 if(updateBean1 != null) {  
  64.                     File file = new File(patchPath +  "/" + updateBean1.getPatchdownloadpath());  
  65.                     if(file.exists()) {  
  66.                         return RestFulUtil.getInstance().getResuFulBean(updateBean1,  0, "有新版本");  
  67.                     }  
  68.                     updateDao.deleteUpdateInfo(updateBean1);  
  69.                     return RestFulUtil.getInstance().getResuFulBean( null, 1, "没有新版本");  
  70.                 }  
  71.             }  
  72.             return RestFulUtil.getInstance().getResuFulBean(null,  1, "没有新版本");  
  73.         }  
  74.     }  
  75.   
  76.     private UpdateBean createPatch(String md5value, int versioncode, String channelid)  
  77.     {  
  78.   
  79.         File md5File = null;  
  80.         File newFile = null;  
  81.         String patchName = "";  
  82.         File patchFile = null;  
  83.         String versionname = "";  
  84.   
  85.         UpdateBean updateBean = null;  
  86.         int newVersionCode = 1;  
  87.         String newVersionName = "";  
  88.   
  89.         File file = new File(oldApksPath);  
  90.         if(!file.exists())  
  91.             return null;  
  92.         File[] files = file.listFiles();  
  93.         if(files == null || files.length == 0)  
  94.             return null;  
  95.   
  96.         /** 
  97.          * 根据MD5值找到和客户端相同的版本 
  98.          */  
  99.         for(File f : files)  
  100.         {  
  101.             String fmd5 = CommonUtils.getFileMd5(f);  
  102.             if(fmd5.equals(md5value))  
  103.             {  
  104.                 System.out.print(f.getName() + " md5: " + fmd5);  
  105.                 String[] flag = f.getName().replace(".apk",  "").split("_");  
  106.                 if(flag != null && flag.length ==  4 && flag[3].equals(channelid))  
  107.                 {  
  108.                     versionname = flag[2];  
  109.                     md5File = f;  
  110.                     break;  
  111.                 }  
  112.             }  
  113.         }  
  114.         if(md5File == null)  
  115.             return null;  
  116.   
  117.         /** 
  118.          * 根据渠道获取当前最新版本 
  119.          */  
  120.         File nfile = new File(newApkPath);  
  121.         if(!nfile.exists())  
  122.             return null;  
  123.         File[] nfiles = nfile.listFiles();  
  124.         if(nfiles == null || nfiles.length ==  0)  
  125.             return null;  
  126.   
  127.         for(File nf : nfiles)  
  128.         {  
  129.             String[] flag = nf.getName().replace(".apk", "").split( "_");  
  130.             if(flag != null && flag.length ==  4 && flag[3].equals(channelid))  
  131.             {  
  132.                 System.out.println("渠道:" + channelid + " 的当前最新版本" + nf.getName());  
  133.                 newFile = nf;  
  134.                 newVersionCode = Integer.parseInt(flag[1]);  
  135.                 newVersionName = flag[2];  
  136.                 patchName = patchPath + "/" + nf.getName().replace( ".apk", "") + "_patch_" + versioncode + ".patch";  
  137.                 break;  
  138.             }  
  139.         }  
  140.         if(newfile == null)  
  141.             return null;  
  142.   
  143.         System.out.println("oldfile:" + md5File.getAbsolutePath());  
  144.         System.out.println("newfile:" + newFile.getAbsolutePath());  
  145.         System.out.println("patchfile:" + patchName);  
  146.   
  147.         int result = BsDiffYwl5320Util.getInstance().bsDiffFile(md5File.getAbsolutePath(), newFile.getAbsolutePath(), patchName);  
  148.         if(result != 0)  
  149.             return null;  
  150.   
  151.         patchFile = new File(patchName);  
  152.         if(!patchFile.exists())  
  153.             return null;  
  154.   
  155.         updateBean = new UpdateBean();  
  156.         updateBean.setMd5value(md5value);  
  157.         updateBean.setVersioncode(versioncode);  
  158.         updateBean.setVersionName(versionname);  
  159.         updateBean.setNewversioncode(newVersionCode);  
  160.         updateBean.setNewversionName(newVersionName);  
  161.         updateBean.setFilesize(md5File.length());  
  162.         updateBean.setPatchsize(patchFile.length());  
  163.         updateBean.setDownloadpath(newFile.getName());  
  164.         updateBean.setPatchdownloadpath(patchFile.getName());  
  165.         updateBean.setChannelid(channelid);  
  166.   
  167.         return updateBean;  
  168.     }  
  169.   
  170. }  
package com.ywl5320.appservice.service;

import com.ywl5320.appservice.bean.RestFulBean;
import com.ywl5320.appservice.bean.UpdateBean;
import com.ywl5320.appservice.dao.UpdateDao;
import com.ywl5320.appservice.util.CommonUtils;
import com.ywl5320.appservice.util.RestFulUtil;
import com.ywl5320.bsdiff.BsDiffYwl5320Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;

/**
 * Created by ywl5320 on 2017/10/26.
 */
@Transactional
public class UpdateService {

    @Autowired
    private UpdateDao updateDao;

    static String oldApksPath = "E:/source/oldversion";
    static String newApkPath = "E:/source/newversion";
    static String patchPath = "E:/source/patch";

    static
    {
        if(CommonUtils.getOsName().contains("linux"))//linux操作系统
        {
            oldApksPath = "/usr/dev/sources/oldversion";
            newApkPath = "/usr/dev/sources/newversion";
            patchPath = "/usr/dev/sources/patch";
        }
        else if(CommonUtils.getOsName().contains("windows"))//window操作系统
        {
            oldApksPath = "E:/source/oldversion";
            newApkPath = "E:/source/newversion";
            patchPath = "E:/source/patch";
        }
    }

    public RestFulBean<UpdateBean> checkUpdate(String md5value, int versioncode, String channelid){

        UpdateBean updateBean = updateDao.getUpdateInfo(md5value, versioncode, channelid);
        if(updateBean != null)
        {
            File file = new File(patchPath + "/" + updateBean.getPatchdownloadpath());
            if(file.exists()) {
                return RestFulUtil.getInstance().getResuFulBean(updateBean, 0, "有新版本");
            }
            //todo 不存在可以再生产增量包或删除此条记录
            updateDao.deleteUpdateInfo(updateBean);
            return RestFulUtil.getInstance().getResuFulBean(null, 1, "没有新版本");
        }
        else
        {
            updateBean = createPatch(md5value, versioncode, channelid);
            if(updateBean != null)
            {
                updateDao.saveUpdateInfo(updateBean);
                UpdateBean updateBean1 = updateDao.getUpdateInfo(md5value, versioncode, channelid);
                if(updateBean1 != null) {
                    File file = new File(patchPath + "/" + updateBean1.getPatchdownloadpath());
                    if(file.exists()) {
                        return RestFulUtil.getInstance().getResuFulBean(updateBean1, 0, "有新版本");
                    }
                    updateDao.deleteUpdateInfo(updateBean1);
                    return RestFulUtil.getInstance().getResuFulBean(null, 1, "没有新版本");
                }
            }
            return RestFulUtil.getInstance().getResuFulBean(null, 1, "没有新版本");
        }
    }

    private UpdateBean createPatch(String md5value, int versioncode, String channelid)
    {

        File md5File = null;
        File newFile = null;
        String patchName = "";
        File patchFile = null;
        String versionname = "";

        UpdateBean updateBean = null;
        int newVersionCode = 1;
        String newVersionName = "";

        File file = new File(oldApksPath);
        if(!file.exists())
            return null;
        File[] files = file.listFiles();
        if(files == null || files.length == 0)
            return null;

        /**
         * 根据MD5值找到和客户端相同的版本
         */
        for(File f : files)
        {
            String fmd5 = CommonUtils.getFileMd5(f);
            if(fmd5.equals(md5value))
            {
                System.out.print(f.getName() + " md5: " + fmd5);
                String[] flag = f.getName().replace(".apk", "").split("_");
                if(flag != null && flag.length == 4 && flag[3].equals(channelid))
                {
                    versionname = flag[2];
                    md5File = f;
                    break;
                }
            }
        }
        if(md5File == null)
            return null;

        /**
         * 根据渠道获取当前最新版本
         */
        File nfile = new File(newApkPath);
        if(!nfile.exists())
            return null;
        File[] nfiles = nfile.listFiles();
        if(nfiles == null || nfiles.length == 0)
            return null;

        for(File nf : nfiles)
        {
            String[] flag = nf.getName().replace(".apk", "").split("_");
            if(flag != null && flag.length == 4 && flag[3].equals(channelid))
            {
                System.out.println("渠道:" + channelid + " 的当前最新版本" + nf.getName());
                newFile = nf;
                newVersionCode = Integer.parseInt(flag[1]);
                newVersionName = flag[2];
                patchName = patchPath + "/" + nf.getName().replace(".apk", "") + "_patch_" + versioncode + ".patch";
                break;
            }
        }
        if(newfile == null)
            return null;

        System.out.println("oldfile:" + md5File.getAbsolutePath());
        System.out.println("newfile:" + newFile.getAbsolutePath());
        System.out.println("patchfile:" + patchName);

        int result = BsDiffYwl5320Util.getInstance().bsDiffFile(md5File.getAbsolutePath(), newFile.getAbsolutePath(), patchName);
        if(result != 0)
            return null;

        patchFile = new File(patchName);
        if(!patchFile.exists())
            return null;

        updateBean = new UpdateBean();
        updateBean.setMd5value(md5value);
        updateBean.setVersioncode(versioncode);
        updateBean.setVersionName(versionname);
        updateBean.setNewversioncode(newVersionCode);
        updateBean.setNewversionName(newVersionName);
        updateBean.setFilesize(md5File.length());
        updateBean.setPatchsize(patchFile.length());
        updateBean.setDownloadpath(newFile.getName());
        updateBean.setPatchdownloadpath(patchFile.getName());
        updateBean.setChannelid(channelid);

        return updateBean;
    }

}


五、action层UpdateAction.java

[java] view plain copy print?
  1. package com.ywl5320.appservice.action;  
  2.   
  3. import com.ywl5320.appservice.bean.RestFulBean;  
  4. import com.ywl5320.appservice.bean.UpdateBean;  
  5. import com.ywl5320.appservice.bean.UserBean;  
  6. import com.ywl5320.appservice.service.UpdateService;  
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.bind.annotation.RequestMethod;  
  11. import org.springframework.web.bind.annotation.ResponseBody;  
  12.   
  13. /** 
  14.  * Created by ywl5320 on 2017/10/26. 
  15.  */  
  16. @Controller  
  17. @RequestMapping("/update")  
  18. public class UpdateAction {  
  19.   
  20.     @Autowired  
  21.     private UpdateService updateService;  
  22.   
  23.     @ResponseBody  
  24.     @RequestMapping(value="/checkupdate.do", method= RequestMethod.GET)  
  25.     public RestFulBean<UpdateBean> loginByPwd(String md5value, int versioncode, String channelid)  
  26.     {  
  27.         System.out.println("md5value:" + md5value);  
  28.         return updateService.checkUpdate(md5value, versioncode, channelid);  
  29.     }  
  30.   
  31. }  
package com.ywl5320.appservice.action;

import com.ywl5320.appservice.bean.RestFulBean;
import com.ywl5320.appservice.bean.UpdateBean;
import com.ywl5320.appservice.bean.UserBean;
import com.ywl5320.appservice.service.UpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by ywl5320 on 2017/10/26.
 */
@Controller
@RequestMapping("/update")
public class UpdateAction {

    @Autowired
    private UpdateService updateService;

    @ResponseBody
    @RequestMapping(value="/checkupdate.do", method= RequestMethod.GET)
    public RestFulBean<UpdateBean> loginByPwd(String md5value, int versioncode, String channelid)
    {
        System.out.println("md5value:" + md5value);
        return updateService.checkUpdate(md5value, versioncode, channelid);
    }

}

六、测试

6.1、创建新版本apk放到newversion文件夹下:



6.2、把旧版本apk放到oldversion文件夹下:



6.3、访问接口:


服务端生成差分包就完成了。

七、tomact配置虚拟路径:

进入tomact根目录下的conf文件夹,编辑server.xml,

在<Host></Host>之间添加:

[plain] view plain copy print?
  1. <Context docBase="E:\source\patch"  reloadable="true"  debug="0" path="apk/update/patch"/>  
<Context docBase="E:\source\patch"  reloadable="true"  debug="0" path="apk/update/patch"/>

八、发布

上一步配置了虚拟路径后,还需要发布重启tomact后才能访问

8.1、生成war包:






8.2、发布到tomact

复制生成的AppService.war到tomact的webapps文件夹下:



8.3、启动tomact

在tomact的bin目录下收入命令:startup.bat



8.4、测试下载

下载地址:

[plain] view plain copy print?
  1. http://192.168.1.138:8080/apk/update/patch/app_2_v1.0.1_xiaomi_patch_1.patch  
http://192.168.1.138:8080/apk/update/patch/app_2_v1.0.1_xiaomi_patch_1.patch


OK,这样就实现了增量更新,这里是以Windows举的例子,Linux原理也是一样的,配置好tomact和mysql,添加.so环境变量和下载虚拟地址就可以了。

客户端下载用的OKhttp3,这里就不接受了,不然又是一大堆截图和代码,客户端完整代码也在GitHub上,可以下载来看看。


源码下载 GitHub  AppServiceRestFul   喜欢就请start一下吧~,