从URL不包含文件的后缀名 [英] Filename from URL not containing filename suffix

查看:267
本文介绍了从URL不包含文件的后缀名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个URL下载文件除了我不知道什么类型的文件将会和我使用不具备在它结束/random.file的URL,所以我不能解析URL作为文件名。
目前我使用了Android的下载管理器,它的工作原理巨星和手段,我没有处理下载,但我看不出反正来获取文件名出文件下载它的。如果我在Firefox加载相同的URL,例如,它要求。下载文件:Nameoffile.extension

I need to download a file from a URL except I don't know what type the file will be and the URL I am using doesn't have /random.file at the end of it so I can't parse the url for the filename. At the moment I'm using the Android download manager which works greats and means I don't have handle the download but I can't see anyway to get the file name out of the file its downloading. If I load the same url in Firefox for example it asks 'Download file: Nameoffile.extension'.

有没有办法对我来说,复制这一行为之前,我下载的文件获取文件名?

Is there a way for me to replicate this behaviour and get the file name before I download the file?

推荐答案

最后我用一个AsyncTask的手动检索文件名,并将其传递到下载管理器,如果它可以帮助任何人,这是我做到了(我的网址

I ended up using an ASyncTask to manually retrieve the file name and passing it to the download manager, if it helps anyone this is how I did it (my url went through a number of redirects before the actual file download):

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); 

                try {
                    for(int i = 0; i < 10; i++)
                    {
                        url = new URL(conn.getHeaderField("Location")); 
                        conn = (HttpURLConnection) url.openConnection();
                        conn.connect();
                        conn.setInstanceFollowRedirects(false);
                    }
                } catch (Exception e) {
                }

                String depo = conn.getHeaderField("Content-Disposition");
                String depoSplit[] = depo.split(";");
                int size = depoSplit.length;
                for(int i = 0; i < size; i++)
                {
                    if(depoSplit[i].startsWith("filename="))
                    {
                        filename = depoSplit[i].replace("filename=", "").replace("\"", "").trim();
                        Global.error(filename);
                        i = size;
                    }
                }
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                } catch (IOException e) {
                }
            return filename;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
    }
}

这篇关于从URL不包含文件的后缀名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆