Get file name from URL

前端 未结 27 1565
[愿得一人]
[愿得一人] 2020-11-28 02:24

In Java, given a java.net.URL or a String in the form of http://www.example.com/some/path/to/a/file.xml , what is the easiest way to g

27条回答
  •  时光取名叫无心
    2020-11-28 03:17

    Here is the simplest way to do it in Android. I know it will not work in Java but It may help Android application developer.

    import android.webkit.URLUtil;
    
    public String getFileNameFromURL(String url) {
        String fileNameWithExtension = null;
        String fileNameWithoutExtension = null;
        if (URLUtil.isValidUrl(url)) {
            fileNameWithExtension = URLUtil.guessFileName(url, null, null);
            if (fileNameWithExtension != null && !fileNameWithExtension.isEmpty()) {
                String[] f = fileNameWithExtension.split(".");
                if (f != null & f.length > 1) {
                    fileNameWithoutExtension = f[0];
                }
            }
        }
        return fileNameWithoutExtension;
    }
    

提交回复
热议问题