【问题标题】:Android: How do I download a file from a dynamic url webviewAndroid:如何从动态 url webview 下载文件
【发布时间】:2013-11-19 11:50:16
【问题描述】:

在我的应用程序中,我使用 webview 导航到站点,使用 javascript 自动填写 web 表单,然后提交以获取指向 CSV 导出文件的链接。 链接如下所示:XYZ.com/TEST/index/getexport?id=130。

我想下载此 URL 指向的文件,然后在完成后将其读入本地数据库,但我无法下载链接的文件。

如果我只是尝试在 webview 中打开 URL,我会收到来自网页的错误消息,告诉我不存在此类文件。

如果我使用下载管理器自己下载,源代码会下载为 html 文件,而不是关联的 .csv 文件。

我可以使用 ACTION_VIEW 意图打开网址,并且浏览器 (chrome) 会下载正确的文件,但这样我就不会收到下载完成时间的通知。

对如何下载我的 .CSV 文件有任何想法吗?

【问题讨论】:

    标签: android webview


    【解决方案1】:

    要从 webview 下载文件,请使用:

    mWebView.setDownloadListener(new DownloadListener(){ 
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength){ 
            Intent i = new Intent(Intent.ACTION_VIEW); 
            i.setData(Uri.parse(url));
            startActivity(i); 
        } 
    });
    

    希望这会有所帮助。

    【讨论】:

    • 放在哪里,还有哪些需要为WebView设置定义的东西?
    【解决方案2】:

    您可以求助于使用 AsyncTask 从 url 手动下载文件。

    这里是背景部分:

    @Override
    protected String doInBackground(Void... params) {
        String filename = "inputAFileName";
    
        HttpURLConnection c;
        try {
            URL url = new URL("http://someurl/" + filename);
            c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
        } catch (IOException e1) {
            return e1.getMessage();
        }
    
        File myFilesDir = new File(Environment
                .getExternalStorageDirectory().getAbsolutePath()
                + "/Download");
    
        File file = new File(myFilesDir, filename);
    
        if (file.exists()) {
            file.delete();
        }
    
        if ((myFilesDir.mkdirs() || myFilesDir.isDirectory())) {
            try {
                InputStream is = c.getInputStream();
                FileOutputStream fos = new FileOutputStream(myFilesDir
                        + "/" + filename);
    
                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();
    
            } catch (Exception e) {
                return e.getMessage();
            }
    
            if (file.exists()) {
                return "File downloaded!";
            } else {
                 Log.e(TAG, "file not found");
            }
        } else {
            Log.e(TAG, "unable to create folder");
        }
    }
    

    也许重构它以便返回文件是有意义的。然后在下载完成后立即在 onPostExecute 中获取文件作为参数。

    【讨论】:

    • 我已经研究过这种方法,但我的下载 URL 不包含文件名。
    • 我认为,如果在 url 上发出 GET 会产生一个文件,它仍然可以工作。当然,您必须相应地更改代码,为文件创建一个名称,以便可以在手机上创建它。
    • 刚试了一下,它下载的是html源代码,而不是我要的文件。这与原生浏览器所做的有什么不同?
    • 如果您在浏览器中使用相同的地址,您会得到 csv?看起来响应的标题不正确...
    • 是的——如果我用 ACTION.VIEW 把它扔到 Android 的 Chrome 上,或者甚至复制/粘贴到 PC 浏览器中,正确的下载就会触发。我无法控制网页的内容,这让事情变得有点棘手......
    猜你喜欢
    • 2014-03-18
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多