Here you can find the source of getFileNameArray(String url)
Parameter | Description |
---|---|
url | the url |
public static String[] getFileNameArray(String url)
//package com.java2s; /*/*from w w w.j av a 2 s . com*/ * Copyright 2012 - 2015 Manuel Laggner * * 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; import java.net.URISyntaxException; import java.net.URL; public class Main { /** * get the correct name/extension/filename of url (even with parameters! - commons-io CANNOT) * * @param url * the url * @return basename/ext/filename array */ public static String[] getFileNameArray(String url) { String[] ret = new String[] { "", "", "" }; // URL: "http://photosaaaaa.net/photos-ak-snc1/v315/224/13/659629384/s659629384_752969_4472.jpg?asdf=jklo" String filename = ""; String path = ""; // PATH: /photos-ak-snc1/v315/224/13/659629384/s659629384_752969_4472.jpg?asdf=jklo try { url = getURIEncoded(url).toString(); path = new URL(url).getPath(); } catch (Exception e) { return ret; } // 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); ret = new String[] { name, extension, filename }; } else { // no extension (eg youtube) String name = lastPartContents[0]; ret = new String[] { name, "", name }; } } return ret; } /** * Casts url string to URI, and does the correct encoding (rfc2396) of query string ONLY (eg "|" character). URLEncoder encodes everything which * might break commons.http * * @param url * the url as string * @return URI object * @throws URISyntaxException * if url could not be parsed / invalid */ public static URI getURIEncoded(String url) throws URISyntaxException { String[] trArr = url.split("://"); return new URI(trArr[0], "//" + trArr[1], null); } }