15.1-单线程下载

OkHttp下载

     private void testOkHttp() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request build = new Request.Builder()
                .url(downLoadUrl)
                .build();
        Call newCall = okHttpClient.newCall(build);
        newCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                Log.d(TAG, "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

//                httpUrlConnection.getContentLength();
                //文件的总长度
                long maxSize= response.body().contentLength();
                mProgressbar.setMax((int) maxSize);
                Log.d(TAG, "onResponse: maxSize="+maxSize);
                // 下载 outputStream inputStream
                InputStream inputStream = response.body().byteStream();

                String sdPath = Environment.getExternalStorageDirectory()+ File.separator+"banmi_330.apk";
                File file = new File(sdPath);
                //当文件不存在,创建出来
                if(!file.exists()){
                    file.createNewFile();
                }
                FileOutputStream outputStream = new FileOutputStream(file);

                byte [] bytes = new byte[1024];
                int readLength=0;
                long currLength=0;
                while ((readLength=inputStream.read(bytes))!=-1) {

                    outputStream.write(bytes,0,readLength);
                    currLength+=readLength;
//                    int progress = (int) (currLength*100/maxSize);
//                    Log.d(TAG, "onResponse: progress="+progress+"%");

                    mProgressbar.setProgress((int) currLength);
                }

                outputStream.close();
                inputStream.close();

            }
        });
    }

httpurlconnection下载


    /**
     * httpurlconnection
     * <p>
     * 输出流 outputstream
     * 输入流 inputStream
     */
    private void downLoadFile() {

        new Thread() {
            @Override
            public void run() {
                super.run();
                InputStream inputStream = null;
                FileOutputStream outputStream = null;
                try {

                    String filename = apkUrl.substring(apkUrl.lastIndexOf("/") + 1);

                    File file = new File(targetFilePath + filename);

                    HttpURLConnection connection = getConnection(apkUrl);

                    int fileLength = connection.getContentLength();

                    inputStream = connection.getInputStream();

                    outputStream = new FileOutputStream(file);

                    byte[] bytes = new byte[8 * 1024];
                    int readLength;
                    int currloadSize = 0;
                    while ((readLength = inputStream.read(bytes)) != -1) {

                        outputStream.write(bytes, 0, readLength);

                        currloadSize += readLength;


                        Log.d(TAG, "run: readlength=" + ((100 * currloadSize) / fileLength) + "%");
                    }

                    Log.d(TAG, "run: 写入文件完成");

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {

                    try {
                        inputStream.close();
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }


            }
        }.start();
    }

    public static HttpURLConnection getConnection(String fileUrl) throws IOException {
        URL url = new URL(fileUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(10 * 1000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
        conn.setRequestProperty("Accept-Language", "zh-CN");
        conn.setRequestProperty("Referer", fileUrl);
        conn.setRequestProperty("Charset", "UTF-8");
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Accept-Encoding", "identity");
        return conn;
    }
发布了118 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/chentaishan/article/details/104940633
今日推荐