I’m downloading an ePub file from a URL.
Now I want to implement a mechanism by which if user tries to re-download the same file, he should get warning/error message and that file should not be downloaded again.
To implement this, I need to check the name of the file present in my library with the name of the file user is trying to download.
But I just have this download link, and not the file name.
How to get the name of the file before download in order to compare it with the existing file?
In android you can use the guessFileName() method:
Alternatively, a simplistic solution in Java could be:
(Assuming your url is in the format:
http://xxxxxxxxxxxxx/filename.ext)UPDATE March 23, 2018
This question is getting lots of hits and someone commented my ‘simple’ solution does not work with certain urls so I felt the need to improve the answer.
In case you want to handle more complex url pattern, I provided a sample solution below. It gets pretty complex quite quickly and I’m pretty sure there are some odd cases my solution still can’t handle but nevertheless here it goes:
This method can handle these type of input:
You can find the whole source code here: https://ideone.com/uFWxTL