【转】Java生成plist下载ipa文件

我们在上传ipa想要安装的时候必须要通过plist文件去下载,并且还要遵循 itms-services协议。 
意思就是,第一步我们要生成一个plist文件, 
第二步生成一个html文件,用来指向plist文件 

下面是生成plist文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>items</key>
        <array>
            <dict>
                <key>assets</key>
                <array>
                    <dict>
                        <key>kind</key>
                        <string>software-package</string>
                        <key>url</key>
                        <string>http://127.0.0.1/project/upload/files/20160504201048174_7836_19.ipa</string>
                    </dict>
                </array>
                <key>metadata</key>
                <dict>
                    <key>bundle-identifier</key>
                    <string>cn.vrv.im-inhouse</string>
                    <key>bundle-version</key>
                    <string>1.0.7</string>
                    <key>kind</key>
                    <string>software</string>
                    <key>subtitle</key>
                    <string>下载</string>
                    <key>title</key>
                    <string></string>
                </dict>
            </dict>
        </array>
    </dict>
</plist>

上面重要的地方有两点

  1. url:这个参数是为了找到你自己上传的ipa文件;
  2. bundle-identifier:这个参数是开发者账号用户名,可以为空或任意,区别在于安装的过程中有无图标和进度

下面是生成html文件,通过html的方式下载这个ipa文件。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下载</title>
<script type="text/javascript"> var url = 'http://127.0.0.1:80//upload/plists/20160606143426371_63551_1.plist'; window.location.href = "itms-services://?action=download-manifest&url=" + url; </script>
</head>
<body></body>
</html>

这样只要我们只要访问这个html地址,就可以自动下载ipa文件了。 
注意:访问这个html文件的时候必须是基于HTTPS的,所以这就需要有一台https服务器 
所以我们只要按照上面的格式,把plist文件,和html文件生成好,就可以安装了。

下面通过写文件的方式生成,请看代码

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/** * * @author ZSL * */
public class CreateUtil {
    public static String createPlist() throws IOException{
        System.out.println("==========开始创建plist文件");
        //这个地址应该是创建的服务器地址,在这里用生成到本地磁盘地址
        final String path = "D:/upload/plists/";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String plistFile = "a.plist";
        final String PLIST_PATH = path + plistFile;
        file = new File(PLIST_PATH);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String plist = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                 + "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
                 + "<plist version=\"1.0\">\n" + "<dict>\n"
                 + "<key>items</key>\n" 
                 + "<array>\n" 
                 + "<dict>\n"
                 + "<key>assets</key>\n" 
                 + "<array>\n" 
                 + "<dict>\n"
                 + "<key>kind</key>\n"
                 + "<string>software-package</string>\n"
                 + "<key>url</key>\n"
                 //你之前所上传的ipa文件路径
                 + "<string>http://127.0.0.1/project/upload/files/20160504201048174_7836_19.ipa</string>\n" 
                 + "</dict>\n" 
                 + "</array>\n"
                 + "<key>metadata</key>\n"
                 + "<dict>\n"
                 + "<key>bundle-identifier</key>\n"
                 //这个是开发者账号用户名,也可以为空,为空安装时看不到图标,完成之后可以看到
                 + "<string>cn.vrv.im-inhouse</string>\n"
                 + "<key>bundle-version</key>\n"
                 + "<string>1.0.7</string>\n"
                 + "<key>kind</key>\n"
                 + "<string>software</string>\n"
                 + "<key>subtitle</key>\n"
                 + "<string>下载</string>\n"
                 + "<key>title</key>\n"
                 + "<string></string>\n" 
                 + "</dict>\n" 
                 + "</dict>\n"
                 + "</array>\n"
                 + "</dict>\n"
                 + "</plist>";
        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 PLIST_PATH;
    }

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

        //这个地址应该是生成的服务器地址,在这里用生成到本地磁盘地址
        final String path = "D:/upload/htmls/";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String plistFile = "a.plist";
        final String PLIST_PATH = path + plistFile;
        file = new File(PLIST_PATH);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String html = "<!DOCTYPE html>\n"
                + "<html lang=\"en\">\n"
                + "<head>\n"
                + "<meta charset=\"UTF-8\">\n"
                + "<title>下载</title>\n"
                + "<script type=\"text/javascript\">\n"
                + "var url = '" + plistPath + "';\n"
                + "window.location.href = \"itms-services://?action=download-manifest&url=\" + url;\n"
                + "</script>\n" 
                + "</head>\n" 
                + "<body></body>\n" 
                + "</html>";

        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";
    }

    public static void main(String[] args) throws IOException {
        String plistPath = CreateUtil.createPlist();
        createHtml(plistPath);
    }

直接运行main函数就可以生成这两个文件。

原文出自:http://www.voidcn.com/blog/moyanxuan_1993_2_24/article/p-6025867.html

posted @ 2016-07-18 14:33  刘贝  阅读(2660)  评论(1编辑  收藏  举报