URL下载

作者: 小慧sir | 来源:发表于2019-07-14 00:15 被阅读0次
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private ProgressBar mProgress;
    /**
     * Hello World!
     */
    private TextView mResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    String  loadUrl="http://cdn.banmi.com/banmiapp/apk/banmi_330.apk";
    public void but_ok(View view) {

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    //切割字符串,得到文件名
                  String filename = loadUrl.substring(loadUrl.lastIndexOf("/") + 1, loadUrl.length());

                    //找到文件路径
                    String filepath = Environment.getExternalStorageDirectory() + File.separator +filename;

                    //创建文件(如果不存在就创建新的)
                    File file = new File(filepath);
                    if (!file.exists()) {
                        file.createNewFile();
                    }

                    URL url = new URL(loadUrl);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    //判断文件是否真实存在
                    final int contentLength = conn.getContentLength();
                    if (contentLength <= 0) {
                        return;
                    }
                    //将文件大小赋值给progress的最大值
                    mProgress.setMax(contentLength);

                    //获取输入输出流
                    InputStream in = conn.getInputStream();
                    FileOutputStream fileOutputStream = new FileOutputStream(file);

                    int len = 0;
                    int sumlength = 0;
                    byte[] bytes = new byte[1024 * 6];
                    while ((len = in.read(bytes)) != -1) {

                        fileOutputStream.write(bytes, 0, len);

                        sumlength += len;
                        //将每次读取的长度累加,除以总长度,就是所读取的长度占总长度之比

                        final int i = (sumlength * 100 / contentLength);
                        final int finalCurrReadLength =sumlength;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mProgress.setProgress(finalCurrReadLength);
                               mResult.setText(i+"%");

                            }
                        });
                    }


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

            }
        }.start();

    }


    private void initView() {
        mProgress = (ProgressBar) findViewById(R.id.progess);
        mResult = (TextView) findViewById(R.id.result);

    }
}

相关文章

网友评论

      本文标题:URL下载

      本文链接:https://www.haomeiwen.com/subject/ikemkctx.html