java解析ipa安装包生成plist文件并生成下载链接

项目说明及实现:
1、通过手机app发布平台发布app,上传app格式为ipa;
2、生成链接供苹果手机浏览器访问并下载安装app;
3、ipa格式app安装时,需要使用plist文件下载该app,并要遵循 itms-services协议;
4、生成一个html文件,用来指向plist文件;或后台重定向指向plist文件,由plist下载ipa文件。

具体代码如下:
代码一:生成plist文件的方法

public String createPlist(String fileName, String version) throws IOException {
        System.out.println("==========开始创建plist文件");
        //plist和ipa文件在服务器中的路径,此处为window路径,linux路径有所差别
        final String path = "D:\\project\\files\\";

        String filePath = path + fileName;
        String plistFile = fileName+".plist";
        String plistPath= path + plistFile;
        File file = new File(plistPath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String plist = "\n"
                + "\n"
                + "\n" + "\n"
                + "items\n"
                + "\n"
                + "\n"
                + "assets\n"
                + "\n"
                + "\n"
                + "kind\n"
                + "software-package\n"
                + "url\n"
                //参数url配置ipa文件的下载路径,参数的值在下面一行设置
                + ""+filePath+"\n"
                + "\n"
                + "\n"
                + "metadata\n"
                + "\n"
                + "bundle-identifier\n"
                //参数bundle-identifier是开发者账号用户名,可以为空或任意,区别在于安装的过程中有无图标和进度,参数的值在下面一行设置
                + "cn.vrv.im-inhouse\n"
                + "bundle-version\n"
                + "" + version + "\n"
                + "kind\n"
                + "software\n"
                + "subtitle\n"
                + "下载\n"
                + "title\n"
                + "安装\n"
                + "\n"
                + "\n"
                + "\n"
                + "\n"
                + "";
        try {
            FileOutputStream output = new FileOutputStream(file);
            OutputStreamWriter writer;
            writer = new OutputStreamWriter(output, "UTF-8");
            writer.write(plist);
            writer.close();
            output.close();
        } catch (Exception e) {
            System.err.println("==========创建plist文件异常:" + e.getMessage());
        }
        System.out.println("==========成功创建plist文件");
        return plistFile;
    }

代码二:生成html文件的方法,通过访问静态html的方式访问plist文件,再由plist自动下载ipa文件。除了使用静态html外也可以通过java后台重定向的方式下载ipa文件(查看代码三)。

public String createHtml(String plistPath) {
        System.out.println("==========开始创建html文件");

        //plist和ipa文件在服务器中的路径,此处为window路径,linux路径有所差别
        String path = "D:\\project\\files\\";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String plistFile = "a.plist";
        final String plistPath = path + plistFile;
        file = new File(PLIST_PATH);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String html = "\n"
                + "\n"
                + "\n"
                + "\n"
                + "下载\n"
                + "\n"
                + "\n"
                + "\n"
                + "";

        try {
            FileOutputStream output = new FileOutputStream(file);
            OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
            writer.write(html);
            writer.close();
            output.close();
        } catch (IOException e) {
            System.err.println("==========创建html文件异常:" + e.getMessage());
        }
        System.out.print("==========成功创建html文件");

        return "success";
    }

注意事项:
plist文件实际的下载路径如下:

https://www.eric.com/download/xx.plist

这时,苹果手机的浏览器是无法下载的,需要在上面的下载路径前拼接字符串"itms-services://?action=download-manifest&url=",拼接后完整的访问路径如下:

itms-services://?action=download-manifest&url=https://www.eric.com/download/xx.plist

这时,再通过苹果手机的浏览器访问该路径就可以看到正常的下载安装界面了。

代码三:java后端controller层重定向访问plist下载ipa文件

@RequestMapping("/downloadIpa")
    public void downloadIpa(@RequestParam("filePath") String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception{
        String suffix = filePath.substring(fileName.lastIndexOf(".") + 1);
        if (suffix.equals("plist")) {
            response.sendRedirect("itms-services://?action=download-manifest&url=" + filePath);
        }
    }

注意:重定向路径前需要加上指定内容,具体原因看代码二中的注意事项。。

你可能感兴趣的:(java解析ipa安装包生成plist文件并生成下载链接)