Java URI to File Name getFileNameFromURI(final URI resourceAddress, final boolean usePathStyleUris)

Here you can find the source of getFileNameFromURI(final URI resourceAddress, final boolean usePathStyleUris)

Description

Gets the file name from the URI.

License

Apache License

Parameter

Parameter Description
resourceAddress the file URI
usePathStyleUris a value indicating if the address is a path style URI

Return

the file's name

Declaration

public static String getFileNameFromURI(final URI resourceAddress, final boolean usePathStyleUris) 

Method Source Code

//package com.java2s;
/**/*  w w w  . j a  v a 2 s  .c om*/
 * Copyright Microsoft Corporation
 * 
 * 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.
 */

import java.net.URI;

public class Main {
    /**
     * Gets the file name from the URI.
     * 
     * @param resourceAddress
     *            the file URI
     * @param usePathStyleUris
     *            a value indicating if the address is a path style URI
     * @return the file's name
     */
    public static String getFileNameFromURI(final URI resourceAddress, final boolean usePathStyleUris) {
        // generate an array of the different levels of the path
        final String[] pathSegments = resourceAddress.getRawPath().split("/");

        // usePathStyleUris ? baseuri/accountname/sharename/objectname : accountname.baseuri/sharename/objectname
        final int shareIndex = usePathStyleUris ? 2 : 1;

        if (pathSegments.length - 1 <= shareIndex) {
            // legal file addresses cannot end with or before the sharename
            throw new IllegalArgumentException(String.format("Invalid file address '%s'.", resourceAddress));
        } else {
            // in a legal file address the lowest level is the filename
            return pathSegments[pathSegments.length - 1];
        }
    }
}

Related

  1. getFileName(URI path)
  2. getFileName(URI uri)
  3. getFileName(URI uri)
  4. getFileName(URI uri)
  5. getFileName(URI uri)
  6. getFilenameFromURI(URI uri, boolean preserveExtension)
  7. getFilePart(String uri)
  8. getFileSystemURI(URI uri)