WebView控件之文件下载

通常webview渲染的界面中含有可以下载文件的链接,点击该链接后,应该开始执行下载的操作并保存文件到本地中。webview来下载页面中的文件通常有两种方式:

  1. 自己通过一个线程写java io的代码来下载和保存文件(可控性好)
  1. 调用系统download的模块(代码简单)

1.第一种下载

// 页面下载功能
        mWebView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
                URL = url;
                // 让ProgressDialog显示
                progressBar.show();
                Thread loginThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        InputStream in = null;
                        FileOutputStream fout = null;
                        // 獲取文件全名,包括後綴
                        downloadFile = new File(Environment
                                .getExternalStorageDirectory().getPath()
                                + DirName);
                        if (!downloadFile.exists()) {
                            downloadFile.mkdirs();
                        }
                        String FolderName = getFileName(URL);

                        if (Environment.getExternalStorageState().equals(
                                Environment.MEDIA_MOUNTED)) {

                            sdFile = new File(downloadFile, FolderName);

                            if (sdFile.exists()) {
                                if (progressBar.isShowing()) {
                                    progressBar.dismiss();
                                }
                                openFile(mContext, sdFile);
                            } else {
                                try {
                                    // String[] mimetypeName =
                                    // FolderName.split(".");
                                    // String HouZui = "."
                                    // + mimetypeName[mimetypeName.length - 1];
                                    URL httpUrl = new URL(URL);
                                    HttpURLConnection conn = (HttpURLConnection) httpUrl
                                            .openConnection();
                                    conn.setDoInput(true);
                                    conn.setDoOutput(true);
                                    in = conn.getInputStream();
                                    FileLen = conn.getContentLength();// 根据响应获取文件大小
                                    if (Environment.getExternalStorageState()
                                            .equals(Environment.MEDIA_MOUNTED)) {

                                        sdFile = new File(downloadFile,
                                                FolderName);
                                        fout = new FileOutputStream(sdFile);
                                    } else {
                                    }
                                    byte[] buffer = new byte[1024];
                                    int len;
                                    int DownLoadLen = 0;
                                    while ((len = in.read(buffer)) != -1) {

                                        fout.write(buffer, 0, len);
                                        // DownLoadLen += len;
                                        // progressBar.setProgress((int)((DownLoadLen
                                        // / FileLen) * 100));
                                    }
                                    if (progressBar.isShowing()) {
                                        progressBar.dismiss();
                                    }
                                    openFile(mContext, sdFile);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                } finally {
                                    if (progressBar.isShowing()) {
                                        progressBar.dismiss();
                                    }
                                    if (in != null) {
                                        try {
                                            in.close();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    if (fout != null) {
                                        try {
                                            fout.close();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
                loginThread.start();
            }
        });

第二种下载方式

class MyDownloadListenter implements DownloadListener{
 
 @Override
 public void onDownloadStart(String url, String userAgent,
 String contentDisposition, String mimetype, long contentLength) {
 System.out.println("url ==== >" + url);
 //new HttpThread(url).start();
  
 Uri uri = Uri.parse(url);
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);
 }
  
 }

你可能感兴趣的:(WebView控件之文件下载)