下载pdf或word文档

private void okDownFile(final String serveletUrl, final String fileName) {
progressDialog = new ProgressDialog(act);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.show();

    //新建一个File,传入文件夹目录
    String SDPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/bdbook/";
    File file1 = new File(SDPath);
    //判断文件夹是否存在,如果不存在就创建,否则不创建
    if (!file1.exists()) {
        //通过file的mkdirs()方法创建目录中包含却不存在的文件夹
        file1.mkdirs();
    }

    OkHttpClient okHttpClient = new OkHttpClient();
    try {
        Request request = new Request.Builder().url(serveletUrl).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("SS", "下载失败");
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream is = null;
                byte[] buf = new byte[2048];
                int len = 0;
                FileOutputStream fos = null;
                String SDPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/bdbook/";
                try {
                    is = response.body().byteStream();
                    long total = response.body().contentLength();
                //  File file = new File(SDPath, serveletUrl.substring(serveletUrl.lastIndexOf("/") + 1));

                    File file = new File(SDPath,fileName);

                    fos = new FileOutputStream(file);
                    long sum = 0;
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                        sum += len;
                        int progress = (int) (sum * 1.0f / total * 100);
                        Log.i("SS", "progress=" + progress);
                        Message msg = handler.obtainMessage();
                        msg.what = 1;
                        msg.arg1 = progress;
                        handler.sendMessage(msg);
                    }
                    fos.flush();
                    Log.i("SS", "文件下载成功");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (is != null)
                            is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        if (fos != null)
                            fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    } catch (Exception e) {
        ToastUtils.show(TypeActivity.this, "文件下载失败", Toast.LENGTH_SHORT);
        progressDialog.dismiss();
    }
}


private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 1:
                //进度条的值
                int i = msg.arg1;
                progressDialog.setProgress(i);
        }
        if (msg.arg1 == 100) {
            ToastUtils.show(TypeActivity.this, "文件下载成功", Toast.LENGTH_SHORT);
            progressDialog.dismiss();
        }
    }
};

public static void verifyStoragePermissions(Activity activity) { try {
//检测是否有写的权限 int permission =
ActivityCompat.checkSelfPermission(activity,
“android.permission.WRITE_EXTERNAL_STORAGE”); if (permission !=
PackageManager.PERMISSION_GRANTED) { // 没有写的权限,去申请写的权限,会弹出对话框
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE); } } catch (Exception e) {
e.printStackTrace(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值