Fetch: How to get download file name

Created on 23 Jul 2019  路  4Comments  路  Source: tonyofrancis/Fetch

I want to get the download file name just before starting the download process.
I tried the following:

fetch.getServerResponse(url, new HashMap<String, List<String>>(), new Func<Downloader.Response>() {
            @Override
            public void call(@NotNull Downloader.Response result) {
                Iterator<String> keys = result.getResponseHeaders().keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    List<String> values = result.getResponseHeaders().get(keys);
                    Log.e(Constants.TAG, key + ":" + ((values != null) ? Arrays.toString(values.toArray()) : "null"));
                }
            }
        }, new Func<Error>() {
            @Override
            public void call(@NotNull Error result) {
                Log.e(Constants.TAG, result.getThrowable().getMessage(), result.getThrowable());
            }
        });

Output:

2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: accept-ranges:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: cache-control:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: connection:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: content-length:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: content-type:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: date:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: etag:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: expires:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: last-modified:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: server:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: strict-transport-security:null
2019-07-23 21:20:19.190 14274-14274/com.radioactive.soundloader E/SoundLoader: x-android-received-millis:null
2019-07-23 21:20:19.191 14274-14274/com.radioactive.soundloader E/SoundLoader: x-android-response-source:null
2019-07-23 21:20:19.191 14274-14274/com.radioactive.soundloader E/SoundLoader: x-android-selected-protocol:null
2019-07-23 21:20:19.191 14274-14274/com.radioactive.soundloader E/SoundLoader: x-android-sent-millis:null
2019-07-23 21:20:19.191 14274-14274/com.radioactive.soundloader E/SoundLoader: x-content-type-options:null
2019-07-23 21:20:19.191 14274-14274/com.radioactive.soundloader E/SoundLoader: x-xss-protection:null
help wanted

All 4 comments

@MohammadFneish7 Do you have a sample url I can test?. Does it work with another url?

@tonyofrancis Thanks for your reply, currently I'm testing on the following URL:
https://www.androidhive.info/wp-content/uploads/2017/07/androidhive2x.png

I also tried different URL's but neither worked.

@tonyofrancis now I'm solving my problem this way without fetch(Using java.net.URL and org.apache.commons.io.FilenameUtils) :

                    URL urlC = new URL(url);
                    URLConnection con = urlC.openConnection();
                    String fieldValue = con.getHeaderField("Content-Disposition");
                    String filename = null;

                    if (fieldValue == null || !fieldValue.contains("filename=\"")) {
                        filename = FilenameUtils.getName(urlC.getPath());
                        if(filename!=null && filename.length()>0){
                            if(!filename.contains(".") ){
                                String type = con.getHeaderField("Content-Type");
                                if(type!=null && type.length()>0 && type.contains("/")){
                                    filename = filename + "." + type.split("/")[1];
                                }
                            }
                           // File Name Resolved
                            publishProgress(filename);
                        }else{
                           // Failed to Resolve File Name
                            publishProgress();
                        }
                    } else {
                        filename = fieldValue.substring(fieldValue.indexOf("filename=\"") + 10, fieldValue.length() - 1);
                        // File Name Resolved
                        publishProgress(filename);
                    }

I thought that it might be helpful to improve fetch.

@MohammadFneish7 Thanks for sharing. I will look into this more.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

burhanaksendir picture burhanaksendir  路  3Comments

jpvs0101 picture jpvs0101  路  6Comments

DeevD picture DeevD  路  3Comments

Mahan3340 picture Mahan3340  路  4Comments

jpvs0101 picture jpvs0101  路  5Comments