• HttpURLConnection 下载代码


    private int downloadFile(final String apkurl, final String apkname) {        
            Log.e(LOGTAG, "downloadApkBackground..apkurl="+apkurl+"; filePath="+apkname);
            File file = new File(apkname);  
            FileOutputStream out = null;
            InputStream is = null;
            int fileLength = 0;
            long count = 0;
            try{
                URL url = new URL(apkurl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(6*1000);  
                conn.setRequestMethod("GET"); 
                conn.setDoOutput(true); 
                if(file.exists()){
                    count = file.length();
                }
                
                conn.setRequestProperty("User-Agent", "NetFox");
                conn.setRequestProperty("RANGE", "bytes=" + count + "-");
                conn .setRequestProperty("Accept-Encoding", "identity"); 
                out = new FileOutputStream(file, true); 
                is = conn.getInputStream();
                fileLength = conn.getContentLength();  
                Log.e(LOGTAG, "downloadApkBackground...count="+count+"; fileLength="+fileLength);
                
                byte buf[] = new byte[4 * 1024];
                int size = 0;
                
                while ((size = is.read(buf)) != -1) {  //down and cached
                    try {
                        out.write(buf, 0, size);
                        count += size;
                        if(count >= fileLength){
                            return UPDATE_FAIL_WITH_SUCCESS;
                        }
                        
                        //Log.e(LOGTAG, "downloadApkBackground..count="+count);
                        //publishProgress(count, fileLength);   
                        out.flush();
                    } catch (Exception e) {        
                        e.printStackTrace();
                        mDownloadIsError = true;
                        return UPDATE_FAIL_WITH_FAIL_UNKONWN;
                    }
                }
                return UPDATE_FAIL_WITH_SUCCESS;
            }catch (MalformedURLException e) {
                mDownloadIsError = true;
                e.printStackTrace();
            }catch (IOException e) {
                mDownloadIsError = true;
                e.printStackTrace();
            }finally{  
                try{  
                    out.close();  
                    is.close();  
                }  
                catch(Exception e){  
                    mDownloadIsError = true;
                    e.printStackTrace();  
                }  
            }
            
            return UPDATE_FAIL_WITH_FAIL_UNKONWN;
        }
  • 相关阅读:
    python模块搜索路径
    Python数据结构
    Python文件类型
    Python循环语句
    Python条件语句
    python配置文件操作——configparser模块
    python 加密方式(MD5&sha&hashlib)
    python MySQL 获取全部数据库(DATABASE)名、表(TABLE)名
    python sqlite3查看数据库所有表(table)
    027.MFC_映射消息
  • 原文地址:https://www.cnblogs.com/wuyong0818/p/5815240.html
Copyright © 2020-2023  润新知