单击webview中的URL时如何获取文件名

Pra*_*tik 7 android android-webview android-download-manager

当我们在页面中为该文件提供链接时,如何获取我们在服务器上传的文件名?

我正在做的是,我在webview中提供带有文件链接的数据,所以每当用户点击链接需要从服务器下载时,因为我从服务器下载了这个文件,但问题是无法使用DownloadManager获取其确切的类型和名称.我想要这样

在此输入图像描述

在上面看到我在test_androt中为我的文件提供了链接,当我点击它时会给dailog带有文件名的选项,当用户点击WebView URL链接时我不知道如何实现这个文件名.

编辑抱歉忘了提及我的网址是这样的

misc/dnload.php?t1=MzQ0MDA=&t2=MTY5NTUz&t3=MTY5NTUzMTMwMjEyMDNfcGhhcm1hY3kga2V5IGluZm8ucG5n&t4=MTMwMjEyMDM=
Run Code Online (Sandbox Code Playgroud)

ant*_*ycr 10

您可以简单地解析网址并获取最后一个"/"后的文本并将其设为文件名,或者您可以使用以下内容

URLUtil.guessFileName(url, contentDisposition, mimetype);
Run Code Online (Sandbox Code Playgroud)

如下所示,因为我在我的DownloadListener中使用它:

//this stuff goes inside your DownloadListener
@Override
public void onDownloadStart(final String url, String userAgent,final String contentDisposition, final String mimetype, long contentLength) 
{
    String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype); //returns a string of the name of the file THE IMPORTANT PART

    //proceed with download
}
Run Code Online (Sandbox Code Playgroud)

如果您不想在DownloadListener中使用它,您可以使用它而不使用contentDisposition或mimetype,只需为每个参数传递'null',除了url(传递url).

编辑:这只适用于你有一个嵌入了文件名的网址.如果您正在寻找一种更加万无一失的方式,请参阅上面的Pratik答案.


Pra*_*tik 10

感谢Raghunandan的建议,我得到了答案.

在这里,我需要额外的调用来获取下载文件的标题信息.在标题部分,我得到了文件的名称.

此外,我发现此URL中的文件名不包含文件名后缀,通过该文件我获得了有关标题详细信息的更多详细信息,我们可以在请求时获得.

因为我们可以使用这个,URLUtil.guessFileName(url, null, null)但是这将给出我的情况下调用的文件名,我有这样的URL

misc/dnload.php?t1=MzQ0MDA=&t2=MTY5NTUz&t3=MTY5NTUzMTMwMjEyMDNfcGhhcm1hY3kga2V5IGluZm8ucG5n&t4=MTMwMjEyMDM=
Run Code Online (Sandbox Code Playgroud)

所以从上面的链接这将提取dnload.php作为文件名它不是原始文件名,我们下载它刚创建该文件的下载链接.

这里是使用这个的代码我们可以得到文件名它只是一个额外的调用来获取信息,但实际上我们下载,下载我已经使用android内置api到DownloadManager下载文件.

Content-Disposition this is the attribute in header section through which we can get the file name as in attachment form.
Run Code Online (Sandbox Code Playgroud)

它将以这种方式返回信息,Content-Disposition: attachment; filename="fname.ext"所以现在只需要提取文件名

class GetFileInfo extends AsyncTask<String, Integer, String>
{
    protected String doInBackground(String... urls)
    {
                URL url;
                String filename = null;
                try {
                    url = new URL(urls[0]);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.connect();
                conn.setInstanceFollowRedirects(false); 

                String depo = conn.getHeaderField("Content-Disposition");
                String depoSplit[] = depo.split("filename=");
                filename = depoSplit[1].replace("filename=", "").replace("\"", "").trim();
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                } catch (IOException e) {
                }
            return filename;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute();
        // use result as file name
    }
}
Run Code Online (Sandbox Code Playgroud)