天天看点

android自更新时下载出现的问题

之前做过一个电视台app,电视台app每次启动时会访问服务器,判断是否需要下载新版本

但是下载时老是下载失败,apk包总是下载不下来.到底是什么原因呢?

服务器下载接口如下:

android自更新时下载出现的问题

/*** 

     * 下载apk 

     * @param path 

     * @param request 

     * @return 

     * @throws ioexception 

     */  

    @requestmapping(value = "/download"/*, headers = {"content-type=application/json"}*/)  

    public responseentity<byte[]> download( string path,httpservletrequest request) throws ioexception {  

        accesslog accesslog=loginto(request);  

        accesslog.setdescription("下载客户端");  

        if(!valuewidget.isnullorempty(request.getcontenttype())&& request.getcontenttype().tolowercase().contains("application/json")){  

            string requeststr=webservletutil.getrequestquerystr(request, null);  

            system.out.println(requeststr);  

            map querymap=jsonputil.getmapfromjson(requeststr);  

            if(!valuewidget.isnullorempty(querymap)){  

                path=(string) querymap.get("path");  

            }  

        }  

        if(valuewidget.isnullorempty(path)){  

            system.out.println("download failed");  

            accesslog.setoperateresult("下载失败,没有传递path参数");  

            logsave(accesslog, request);  

            return null;  

        string realpath =webservletutil.getuploadpath(request, "upload/download/apk", request  

                .getsession().getservletcontext(), constant2.src_main_webapp);  

        if(!realpath.endswith(file.separator)){  

            realpath=realpath+file.separator;  

        httpheaders headers = new httpheaders();  

        headers.setcontenttype(mediatype.application_octet_stream);  

        string fullpath=realpath+path;  

        system.out.println("download path:"+fullpath);  

        headers.set(constant2.content_disposition,webservletutil.getcontentdisposition(true, path));  

        accesslog.setoperateresult("下载成功,下载文件:"+fullpath+" ,size:"+fileutils.getfilesize2(fullpath));  

        logsave(accesslog, request);  

        return new responseentity<byte[]>(fileutils.getbytes4file(fullpath),  

                                          headers, httpstatus.created);  

    }  

android自更新时下载出现的问题

     *  

     * @param huc 

     * @param sendbytes 

     * @param mode 

     * @param iswrite2file 

     *            : 是否写入文件 

     * @throws exception 

    private static byte[] connection(httpurlconnection huc,  

            boolean iswrite2file, object file, string sizeheadkey)  

            throws exception {  

        int rescode = huc.getresponsecode();  

        if (rescode == httpurlconnection.http_ok) {  

            int contentlength = 0;  

            if (valuewidget.isnullorempty(sizeheadkey)) {// 若header中没有size  

                contentlength = huc.getcontentlength();  

            } else {  

                string sizeheadervalue = huc.getheaderfield(sizeheadkey);  

                if (!valuewidget.isnullorempty(sizeheadervalue)) {  

                    contentlength = integer.parseint(sizeheadervalue);  

                }  

            if (isdetail) {  

                system.out  

                        .println("[connection]contentlength:" + contentlength);  

                responseheaderfields = huc.getheaderfields();  

                string downloadheader = "content-disposition";  

                if (!valuewidget.isnullorempty(responseheaderfields)) {  

                    list<string> contentdispositions = responseheaderfields  

                            .get(downloadheader);  

                    if (!valuewidget.isnullorempty(contentdispositions)) {  

                        string contentdisposition = contentdispositions.get(0);  

                        system.out.println("contentdisposition:"  

                                + contentdisposition);  

                        system.out.println("contentdisposition convertiso2utf:"  

                                + systemhwutil  

                                        .convertiso2utf(contentdisposition));  

                        system.out  

                                .println("contentdisposition convertiso2gbk: "  

                                        + systemhwutil  

                                                .convertiso2gbk(contentdisposition));  

                    }  

                for (object obj : responseheaderfields.keyset()) {  

                    list<string> list = responseheaderfields.get(obj);  

                    if (!valuewidget.isnullorempty(list)) {  

                        system.out.println(obj + " : "  

                                + systemhwutil.formatarr(list, ";"));  

            if (contentlength > 0) {  

                if (isdetail)  

                    system.out  

                            .println("[httpsocketutil.connection]httputil,contentlength:"  

                                    + contentlength);  

                // return readdata(huc);  

                file file2 = null;  

                if (iswrite2file) {  

                    if (file instanceof file) {  

                        file2 = (file) file;  

                        writefilefromlength(huc, contentlength, file2);  

                        if (isdetail) {  

                            system.out.println("download success:"  

                                    + file2.getabsolutepath());  

                        }  

                    } else {  

                        writefilefromlength(huc, contentlength,  

                                (outputstream) file);  

                    return null;  

                } else {  

                    return readdatafromlength(huc, contentlength);  

                    inputstream in = huc.getinputstream();  

                    fileutils.writein2outputcloseall(in, new fileoutputstream(  

                            (file) file));  

                    if (isdetail) {  

                        system.out.println("download success:"  

                                + ((file) file).getabsolutepath());  

                return readdata(huc);  

        } else {  

            system.out.println("response code:" + rescode);  

        return null;  

代码本身是没有逻辑错误的.花了很长时间才找到原因,是定义的response的status code不一致.

android自更新时下载出现的问题
android自更新时下载出现的问题

修改方法:把后台的status code改为200 就ok了