Parse file name from URL before downloading the file

后端 未结 6 1668
眼角桃花
眼角桃花 2020-12-24 01:18

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 messag

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-24 02:05

    You dont really have to compare file names. Just make File object for the file with absolute path and check if file exists.

    protected boolean need2Download(String fileName) {
    
        File basePath = new File(BOOK_STORE_PATH);
    
        File fullPath = new File(basePath, fileName);
    
        if (fullPath.exists())
            return false;
        return true;
    }
    
    protected void downloadFile(String url) {
        String fileName = url.substring(url.lastIndexOf('/') + 1);
    
        if (need2Download(fileName)) {
            // download
        }
    }
    

提交回复
热议问题