Java File Name Extract extractFilenameFromUrlPath(String urlPath)

Here you can find the source of extractFilenameFromUrlPath(String urlPath)

Description

Extract the URL filename from the given request URL path.

License

Apache License

Parameter

Parameter Description
urlPath the request URL path (e.g. "/index.html")

Return

the extracted URI filename (e.g. "index")

Declaration

public static String extractFilenameFromUrlPath(String urlPath) 

Method Source Code

//package com.java2s;
/*//from  ww w. jav  a  2 s  . c  o m
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Extract the URL filename from the given request URL path.
     * Correctly resolves nested paths such as "/products/view.html" as well.
     * @param urlPath the request URL path (e.g. "/index.html")
     * @return the extracted URI filename (e.g. "index")
     */
    public static String extractFilenameFromUrlPath(String urlPath) {
        String filename = extractFullFilenameFromUrlPath(urlPath);
        int dotIndex = filename.lastIndexOf('.');
        if (dotIndex != -1) {
            filename = filename.substring(0, dotIndex);
        }
        return filename;
    }

    /**
     * Extract the full URL filename (including file extension) from the given
     * request URL path. Correctly resolve nested paths such as
     * "/products/view.html" and remove any path and or query parameters.
     * @param urlPath the request URL path (e.g. "/products/index.html")
     * @return the extracted URI filename (e.g. "index.html")
     */
    public static String extractFullFilenameFromUrlPath(String urlPath) {
        int end = urlPath.indexOf('?');
        if (end == -1) {
            end = urlPath.indexOf('#');
            if (end == -1) {
                end = urlPath.length();
            }
        }
        int begin = urlPath.lastIndexOf('/', end) + 1;
        int paramIndex = urlPath.indexOf(';', begin);
        end = (paramIndex != -1 && paramIndex < end ? paramIndex : end);
        return urlPath.substring(begin, end);
    }
}

Related

  1. extractFileNameFromPath(final String filePath, char separatorChar)
  2. extractFileNameFromPath(final String path)
  3. extractFilenameFromPath(String path)
  4. extractFilenameFromURL(String url)
  5. extractFileNameFromUrl(String url)
  6. extractFileNames(String[] fileNames)