Android development download apk download file code

Record

protected File downLoad(String httpUrl) {
        final String apkName = "down.apk";
        File tmpFile = new File("/sdcard/update");
        if (!tmpFile.exists()) {
            tmpFile.mkdir();
        }
        final File file = new File("/sdcard/down/" + apkName);

        try {
            URL url = new URL(httpUrl);
            try {
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                InputStream is = conn.getInputStream();
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buf = new byte[256];
                conn.connect();
                double count = 0;
                if (conn.getResponseCode() >= 500) {
                    Toast.makeText(SplashActivity.this, "连接超时", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    while (count <= 100) {
                        if (is != null) {
                            int numRead = is.read(buf);
                            if (numRead <= 0) {
                                break;
                            } else {
                                Log.e("baby",numRead+":可不可以静默安装");
                                fos.write(buf, 0, numRead);
                            }

                        } else {
                            break;
                        }

                    }
                }

                conn.disconnect();
                fos.close();
                is.close();
                File file1 = new File("/sdcard/update/down.apk");//下载apk后的路径
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("baby",e+":e");
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            Log.e("baby",e+":e1");
            e.printStackTrace();
        }

        return file;
    }

 

Guess you like

Origin blog.csdn.net/congcongguniang/article/details/106017212