Filename from URL not containing filename suffix

2019-04-16 09:08发布

问题:

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?

回答1:

You are better off reading the HTTP Content-Type header on the response and figuring out what type of file it is. File name extensions do not guarantee the type of the file. Content-Disposition: attachment; filename="fname.ext" is another way to do this if you are specific about the file name. Check out the list of HTTP headers for more information.



回答2:

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