Java URL to File Name getFileName(URL extUrl)

Here you can find the source of getFileName(URL extUrl)

Description

get File Name

License

Open Source License

Declaration

public static String getFileName(URL extUrl) 

Method Source Code

//package com.java2s;
/*  This file is part of Openrouteservice.
 *
 *  Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the 
 *  GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 
 *  of the License, or (at your option) any later version.
    /*from w  ww . j  a  va  2 s  .  co  m*/
 *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
    
 *  You should have received a copy of the GNU Lesser General Public License along with this library; 
 *  if not, see <https://www.gnu.org/licenses/>.  
 */

import java.net.URL;

public class Main {
    public static String getFileName(URL extUrl) {
        // URL:
        // "http://photosaaaaa.net/photos-ak-snc1/v315/224/13/659629384/s659629384_752969_4472.jpg"
        String filename = "";
        // PATH:
        // /photos-ak-snc1/v315/224/13/659629384/s659629384_752969_4472.jpg
        String path = extUrl.getPath();
        // Checks for both forward and/or backslash
        // NOTE:**While backslashes are not supported in URL's
        // most browsers will autoreplace them with forward slashes
        // So technically if you're parsing an html page you could run into
        // a backslash , so i'm accounting for them here;
        String[] pathContents = path.split("[\\\\/]");
        if (pathContents != null) {
            int pathContentsLength = pathContents.length;
            System.out.println("Path Contents Length: " + pathContentsLength);
            for (int i = 0; i < pathContents.length; i++) {
                System.out.println("Path " + i + ": " + pathContents[i]);
            }
            // lastPart: s659629384_752969_4472.jpg
            String lastPart = pathContents[pathContentsLength - 1];
            String[] lastPartContents = lastPart.split("\\.");
            if (lastPartContents != null && lastPartContents.length > 1) {
                int lastPartContentLength = lastPartContents.length;
                System.out.println("Last Part Length: " + lastPartContentLength);
                // filenames can contain . , so we assume everything before
                // the last . is the name, everything after the last . is the
                // extension
                String name = "";
                for (int i = 0; i < lastPartContentLength; i++) {
                    System.out.println("Last Part " + i + ": " + lastPartContents[i]);
                    if (i < (lastPartContents.length - 1)) {
                        name += lastPartContents[i];
                        if (i < (lastPartContentLength - 2)) {
                            name += ".";
                        }
                    }
                }
                String extension = lastPartContents[lastPartContentLength - 1];
                filename = name + "." + extension;
                System.out.println("Name: " + name);
                System.out.println("Extension: " + extension);
                System.out.println("Filename: " + filename);
            }
        }
        return filename;
    }
}

Related

  1. getFilename(final URL url, int maxLength)
  2. getFileName(HttpURLConnection conn)
  3. getFileName(String urlStr)
  4. getFilename(String urlString)
  5. getFileName(String urlString)
  6. getFileName(URL url)
  7. getFileName(URL url)
  8. getFileName(URL url)
  9. getFileName(URL url)