In Java, given a java.net.URL
or a String
in the form of http://www.example.com/some/path/to/a/file.xml
, what is the easiest way to g
How about this:
String filenameWithoutExtension = null;
String fullname = new File(
new URI("http://www.xyz.com/some/deep/path/to/abc.png").getPath()).getName();
int lastIndexOfDot = fullname.lastIndexOf('.');
filenameWithoutExtension = fullname.substring(0,
lastIndexOfDot == -1 ? fullname.length() : lastIndexOfDot);
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;
}
Keep it simple :
/**
* This function will take an URL as input and return the file name.
* <p>Examples :</p>
* <ul>
* <li>http://example.com/a/b/c/test.txt -> test.txt</li>
* <li>http://example.com/ -> an empty string </li>
* <li>http://example.com/test.txt?param=value -> test.txt</li>
* <li>http://example.com/test.txt#anchor -> test.txt</li>
* </ul>
*
* @param url The input URL
* @return The URL file name
*/
public static String getFileNameFromUrl(URL url) {
String urlString = url.getFile();
return urlString.substring(urlString.lastIndexOf('/') + 1).split("\\?")[0].split("#")[0];
}
import java.io.*;
import java.net.*;
public class ConvertURLToFileName{
public static void main(String[] args)throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the URL : ");
String str = in.readLine();
try{
URL url = new URL(str);
System.out.println("File : "+ url.getFile());
System.out.println("Converting process Successfully");
}
catch (MalformedURLException me){
System.out.println("Converting process error");
}
I hope this will help you.
If you don't need to get rid of the file extension, here's a way to do it without resorting to error-prone String manipulation and without using external libraries. Works with Java 1.7+:
import java.net.URI
import java.nio.file.Paths
String url = "http://example.org/file?p=foo&q=bar"
String filename = Paths.get(new URI(url).getPath()).getFileName().toString()
I've found that some urls when passed directly to FilenameUtils.getName
return unwanted results and this needs to be wrapped up to avoid exploits.
For example,
System.out.println(FilenameUtils.getName("http://www.google.com/.."));
returns
..
which I doubt anyone wants to allow.
The following function seems to work fine, and shows some of these test cases, and it returns null
when the filename can't be determined.
public static String getFilenameFromUrl(String url)
{
if (url == null)
return null;
try
{
// Add a protocol if none found
if (! url.contains("//"))
url = "http://" + url;
URL uri = new URL(url);
String result = FilenameUtils.getName(uri.getPath());
if (result == null || result.isEmpty())
return null;
if (result.contains(".."))
return null;
return result;
}
catch (MalformedURLException e)
{
return null;
}
}
import java.util.Objects;
import java.net.URL;
import org.apache.commons.io.FilenameUtils;
class Main {
public static void main(String[] args) {
validateFilename(null, null);
validateFilename("", null);
validateFilename("www.google.com/../me/you?trex=5#sdf", "you");
validateFilename("www.google.com/../me/you?trex=5 is the num#sdf", "you");
validateFilename("http://www.google.com/test.png?test", "test.png");
validateFilename("http://www.google.com", null);
validateFilename("http://www.google.com#test", null);
validateFilename("http://www.google.com////", null);
validateFilename("www.google.com/..", null);
validateFilename("http://www.google.com/..", null);
validateFilename("http://www.google.com/test", "test");
validateFilename("https://www.google.com/../../test.png", "test.png");
validateFilename("file://www.google.com/test.png", "test.png");
validateFilename("file://www.google.com/../me/you?trex=5", "you");
validateFilename("file://www.google.com/../me/you?trex", "you");
}
private static void validateFilename(String url, String expectedFilename){
String actualFilename = getFilenameFromUrl(url);
System.out.println("");
System.out.println("url:" + url);
System.out.println("filename:" + expectedFilename);
if (! Objects.equals(actualFilename, expectedFilename))
throw new RuntimeException("Problem, actual=" + actualFilename + " and expected=" + expectedFilename + " are not equal");
}
public static String getFilenameFromUrl(String url)
{
if (url == null)
return null;
try
{
// Add a protocol if none found
if (! url.contains("//"))
url = "http://" + url;
URL uri = new URL(url);
String result = FilenameUtils.getName(uri.getPath());
if (result == null || result.isEmpty())
return null;
if (result.contains(".."))
return null;
return result;
}
catch (MalformedURLException e)
{
return null;
}
}
}