android webview下载附件几种方法

来源:互联网 发布:手机shell是什么软件 编辑:程序博客网 时间:2024/06/05 23:49
下面几种方式都能下载,记录的是稍作修改后的结果。
首先
webView.setDownloadListener(new MyWebViewDownLoadListener());
然后
1、浏览器,不能传递cookie
 private class MyWebViewDownLoadListener implements DownloadListener {        @Override        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) {                        Uri uri = Uri.parse(url);            Intent intent = new Intent(Intent.ACTION_VIEW, uri);            startActivity(intent);                   }    }
2、DownloadManager,可传递cookie
http://www.2cto.com/kf/201205/132327.html
 private class MyWebViewDownLoadListener implements DownloadListener {        @Override        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) {                      
//先判断有无权限,否在未授权时app直接跳出
int perm = MainActivity.this.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE");
if(perm != PackageManager.PERMISSION_GRANTED){
    Toast.makeText(MainActivity.this,"缺少存储权限:手机-设置-应用管理-权限-存储",Toast.LENGTH_LONG).show();
}
else {
    DownloadManager downloadManager = ((DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE));
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
    request.setVisibleInDownloadsUi(true);
    //传cookie
    String CookieStr= CookieManager.getInstance().getCookie(url);
    request.addRequestHeader("Cookie",CookieStr);
    //contentDisposition本身带有att...一串字符,中文和非中文还不一样    
    String fileName;
    if(contentDisposition.indexOf("filename*=UTF-8")!=-1) {
     fileName = contentDisposition.substring(contentDisposition.indexOf("'") + 2, contentDisposition.length());
   }
    else{
        fileName = contentDisposition.substring(contentDisposition.indexOf("=") + 1, contentDisposition.length());
     }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
    //过时的,老版用这句,新版用后面那句
    request.setShowRunningNotification(true);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    downloadManager.enqueue(request);
}
        }    }
实际应用中,获取cookie部分是这样写的,这样不必每下一个文件都取一遍
private String CookieStr;private class MyWebViewClient extends WebViewClient {    @Override    public boolean shouldOverrideUrlLoading(WebView view, String url) {        view.loadUrl(url);        return true;    }    public void onPageFinished(WebView view, String url) {        CookieStr =  CookieManager.getInstance().getCookie(url);        super.onPageFinished(view, url);    }}

3、thread,这个cookie问题没试过
http://blog.csdn.net/u013210620/article/details/47184511
contentDisposition是为了修改文件名后加的
 private class MyWebViewDownLoadListener implements DownloadListener {        @Override        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) {                   new HttpThread(url,contentDisposition).start();        }    }
public class HttpThread extends Thread {    private String url;    private String contentDisposition;    public HttpThread(String url,String contentDisposition) {        super();        this.url = url;        this.contentDisposition=contentDisposition;    }    @Override    public void run() {        super.run();        try {            URL httpUrl = new URL(url);            HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();            connection.setDoInput(true);            connection.setDoOutput(true);            InputStream inputStream = connection.getInputStream();            File downFile = null;            File sdFile = null;            FileOutputStream out = null;            String filename;            Log.e("TAG", "sddd");
            //下面的if有点问题,到这就不执行了,没仔细研究,注释后能过去            //if (Environment.getExternalStorageState().equals(Environment.MEDIA_CHECKING)) {                Log.e("TAG", "sd");                downFile = Environment.getExternalStorageDirectory();                
    //contentDisposition本身带有att...一串字符,中文和非中文还不一样    
    fileName;
    if(contentDisposition.indexOf("filename*=UTF-8")!=-1) {
     fileName = contentDisposition.substring(contentDisposition.indexOf("'") + 2, contentDisposition.length());
   }
    else{
        fileName = contentDisposition.substring(contentDisposition.indexOf("=") + 1, contentDisposition.length());
     }
sdFile = new File(downFile, filename); out = new FileOutputStream(sdFile); // } byte[] b = new byte[6 * 1024]; int len; while ((len = inputStream.read(b)) != -1) { if (out != null) { out.write(b, 0, len); } } if (out != null) { out.close(); } if (inputStream != null) { inputStream.close(); } Log.e("TAG", "scca"); } catch (Exception e) { e.printStackTrace(); } }}

0 0
原创粉丝点击