實用的系統工具類URLUtil

今天在實用webview下載文件的時候,本來是想自己拼接一個文件名的,后來,還是決定直接拿要下載的文件名。

那么怎么獲取文件名呢?

其實系統為我們提供了一個很實用的工具類

android webkit包下的URLUtil工具類

獲取文件名一個方法搞定

gussFileName()

String  fileName = URLUtil.guessFileName(url,contentDisposition,mimetype);

看源碼 第一個參數就是要文件的下載url,一定要是文件的下載url,而不是所在的網頁的url。也就是說,這個地址是可以直接在瀏覽器或者迅雷上面下載的。

第二個參數 Content-disposition 是 MIME 協議的擴展,MIME 協議指示 MIME 用戶代理如何顯示附加的文件

第三個參數 mimetype 返回內容的Mime-type

 /**
     * Guesses canonical filename that a download would have, using
     * the URL and contentDisposition. File extension, if not defined,
     * is added based on the mimetype
     * @param url Url to the content
     * @param contentDisposition Content-Disposition HTTP header or {@code null}
     * @param mimeType Mime-type of the content or {@code null}
     *
     * @return suggested filename
     */
    public static final String guessFileName

這幾個參數在webview的DownloadListener的監聽中返回

        mWebview.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                Log.i(TAG,url + "--" + userAgent + "--" + contentDisposition + "--" + mimetype + "--" + contentLength);
                String fileName = URLUtil.guessFileName(url,contentDisposition,mimetype);
            }
        });

除了guessFileName,URLUtil中還有一些其他比較實用的方法。

比如,下面這個verifyURLEncoding(url)

verifyURLEncoding()

判斷url是否正確的編碼

   /**
     * @return {@code true} if the url is correctly URL encoded
     */
    static boolean verifyURLEncoding(String url) {
        int count = url.length();
        if (count == 0) {
            return false;
        }

        int index = url.indexOf('%');
        while (index >= 0 && index < count) {
            if (index < count - 2) {
                try {
                    parseHex((byte) url.charAt(++index));
                    parseHex((byte) url.charAt(++index));
                } catch (IllegalArgumentException e) {
                    return false;
                }
            } else {
                return false;
            }
            index = url.indexOf('%', index + 1);
        }
        return true;
    }

isValidUrl()

判斷url是否合法

 /**
     * @return {@code true} if the url is valid.
     */
    public static boolean isValidUrl(String url) {
        if (url == null || url.length() == 0) {
            return false;
        }

        return (isAssetUrl(url) ||
                isResourceUrl(url) ||
                isFileUrl(url) ||
                isAboutUrl(url) ||
                isHttpUrl(url) ||
                isHttpsUrl(url) ||
                isJavaScriptUrl(url) ||
                isContentUrl(url));
    }

這個方法的源碼中調用了其他幾個比較實用的方法,比如,對http https的判斷。我們就不需要單獨去寫代碼,直接調用這個工具類的方法就可以了。

    /**
     * @return {@code true} if the url is an http: url.
     */
    public static boolean isHttpUrl(String url) {
        return (null != url) &&
               (url.length() > 6) &&
               url.substring(0, 7).equalsIgnoreCase("http://");
    }

這個是http的判斷,代碼比我們平常自己寫的嚴謹多了。

還有幾個方法也是比較實用的,這里就不一一介紹了,直接源碼里面就可以查看。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容