Assuming I am given a URI, and I want to find the file extension of the file that is returned, what do I have to do in Java.
For example the file at http://www.daml.org/2001/08/baseball/baseball-ont is http://www.daml.org/2001/08/baseball/baseball-ont.owl
When I do
URI uri = new URI(address); URL url = uri.toURL(); String file = url.getFile(); System.out.println(file);
I am not able to see the full file name with .owl
extension, just /2001/08/baseball/baseball-ont
how do I get the file extension as well. ``
Your computer has many different types of files on it, and each one has its own file extension. A file extension is a three- or four-letter identifier found at the end of a file name and following a period. These extensions tell you about the characteristics of a file and its use.
A file with the . C file extension is a plain text C/C++ source code file. It can both hold an entire program's source code in the C or C++ programming language, and be referenced by other files from within a C project.
The full filename is first obtained by selecting the file input and getting its value property. This returns the filename as a string. By the help of split() method, we will split the filename into 2 parts. The first part will be the filename and the second part will be the extension of the file.
At first, I want to make sure you know it's impossible to find out what kind of file a URI links too, since a link ending with .jpg
might let you access a .exe
file (this is especially true for URL's, due to symbolic links and .htaccess files), thus it isn't a rock solid solution to fetch the real extension from the URI if you want to limit allowed file types, if this is what you're going for of course. So, I assume you just want to know what extension a file has based on it's URI even though this isn't completely trustworthy;
You can get the extension from any URI, URL or file path using the method bellow. You don't have to use any libraries or extensions, since this is basic Java functionality. This solution get's the position of the last .
(period) sign in the URI string, and creates a sub-string starting at the position of the period sign, ending at the end of the URI string.
String uri = "http://www.google.com/support/enterprise/static/gsa/docs/admin/70/gsa_doc_set/integrating_apps/images/google_logo.png"; String extension = uri.substring(uri.lastIndexOf("."));
This code sample will above will output the .png
extension from the URI in the extension
variable, note that a .
(period) is included in the extension, if you want to gather the file extension without a prefixed period, increase the substring index by one, like this:
String extension = uri.substring(url.lastIndexOf(".") + 1);
One pro for using this method over regular expressions (a method other people use a lot) is that this is a lot less resource expensive and a lot less heavy to execute while giving the same result.
Additionally, you might want to make sure the URL contains a period character, use the following code to achieve this:
String uri = "http://www.google.com/support/enterprise/static/gsa/docs/admin/70/gsa_doc_set/integrating_apps/images/google_logo.png"; if(uri.contains(".")) { String extension = uri.substring(url.lastIndexOf(".")); }
You might want to improve the functionally even further to create a more robust system. Two examples might be:
I won't cover the solutions for these two features in here, because that isn't what was being asked in the first place.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With