集册 Java实例教程 从URL的文件部分返回文件扩展名。

从URL的文件部分返回文件扩展名。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

471
从URL的文件部分返回文件扩展名。
/**
NowJava.com - 时  代  Java 提供 
**/

/*

 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.

 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

 */

//package com.nowjava;

import java.net.URL;


public class Main {

    /**

     * Returns the file extension from the file part of the URL.

     * The returned file extension include the leading '.' character.

     * <P>

     * For example: if the URL is http://www.sun.com/index.html, the 

     * returned file extension is ".html".

     *

     * @param url the specified URL

     * @return the file extension of the file part of the URL.

     */

    public static String getFileExtensionByURL(URL url) {

        String trimFile = url.getFile().trim();


        if (trimFile == null || trimFile.equals("") || trimFile.equals("/")) {

            return null;

        }


        int strIndex = trimFile.lastIndexOf("/");

        String filePart = trimFile.substring(strIndex + 1,

                trimFile.length());


        strIndex = filePart.lastIndexOf(".");

        if (strIndex == -1 || strIndex == filePart.length() - 1) {

          
展开阅读全文