Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8263941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:16:45+00:00 2026-06-08T04:16:45+00:00

I’m downloading an ePub file from a URL. Now I want to implement a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:16:47+00:00Added an answer on June 8, 2026 at 4:16 am

    In android you can use the guessFileName() method:

    URLUtil.guessFileName(url, null, null)
    

    Alternatively, a simplistic solution in Java could be:

    String fileName = url.substring(url.lastIndexOf('/') + 1);
    

    (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:

    public static String getFileNameFromURL(String url) {
        if (url == null) {
            return "";
        }
        try {
            URL resource = new URL(url);
            String host = resource.getHost();
            if (host.length() > 0 && url.endsWith(host)) {
                // handle ...example.com
                return "";
            }
        }
        catch(MalformedURLException e) {
            return "";  
        }
    
        int startIndex = url.lastIndexOf('/') + 1;
        int length = url.length();
    
        // find end index for ?
        int lastQMPos = url.lastIndexOf('?');
        if (lastQMPos == -1) {
            lastQMPos = length; 
        }
    
        // find end index for #
        int lastHashPos = url.lastIndexOf('#');
        if (lastHashPos == -1) {
            lastHashPos = length;   
        }
    
        // calculate the end index
        int endIndex = Math.min(lastQMPos, lastHashPos);
        return url.substring(startIndex, endIndex);
    }
    

    This method can handle these type of input:

    Input: "null" Output: ""
    Input: "" Output: ""
    Input: "file:///home/user/test.html" Output: "test.html"
    Input: "file:///home/user/test.html?id=902" Output: "test.html"
    Input: "file:///home/user/test.html#footer" Output: "test.html"
    Input: "http://example.com" Output: ""
    Input: "http://www.example.com" Output: ""
    Input: "http://www.example.txt" Output: ""
    Input: "http://example.com/" Output: ""
    Input: "http://example.com/a/b/c/test.html" Output: "test.html"
    Input: "http://example.com/a/b/c/test.html?param=value" Output: "test.html"
    Input: "http://example.com/a/b/c/test.html#anchor" Output: "test.html"
    Input: "http://example.com/a/b/c/test.html#anchor?param=value" Output: "test.html"
    

    You can find the whole source code here: https://ideone.com/uFWxTL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
i want to parse a xhtml file and display in UITableView. what is the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.