如何从uri确定文件的文件扩展名 [英] How to determine the file extension of a file from a uri

查看:136
本文介绍了如何从uri确定文件的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个URI,并且我想查找返回的文件的文件扩展名,我在Java中需要做什么。

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示例 http://www.daml.org/2001/08/baseball上的文件/ baseball-ont http://www.daml。 org / 2001/08 / baseball / baseball-ont.owl

当我这样做时

    URI uri = new URI(address); 
    URL url = uri.toURL();
    String file = url.getFile();
    System.out.println(file);

我无法通过 .owl看到完整的文件名扩展名,只需 / 2001/08 / baseball / baseball-ont 我如何获得文件扩展名。
``

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. ``

推荐答案

首先,我想确保你知道找不到什么样的档案是不可能的URI也链接,因为以 .jpg 结尾的链接可能允许您访问 .exe 文件(特别是对于URL,由于符号链接和.htaccess文件,这是真的,因此,如果要限制允许的文件类型,如果要限制允许的文件类型,则从URI获取真实扩展名不是一个坚如磐石的解决方案你当然要去做什么所以,我假设您只想知道文件基于它的URI的扩展名,即使这不是完全值得信任的;

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;

您可以从任何URI获取扩展名,URL或文件路径使用下面的方法。您不必使用任何库或扩展,因为这是基本的Java功能。此解决方案获取URI字符串中最后一个(句点)的位置,并创建一个从句点符号位置开始的子字符串,最后结束URI字符串。

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("."));

此代码示例将在上面输出 .png 扩展来自扩展变量中的URI,请注意扩展中包含(句点),如果你想收集没有前缀句点的文件扩展名,请将子字符串索引增加1,如下所示:

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.

此外,您可能希望确保URL包含句点字符,请使用以下代码实现此目的:

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:


  • 通过检查URI是否存在来验证URI,或者确保URI的语法有效,可能使用正则表达式。

  • 修剪扩展名以删除不需要的空格。

我不会在这里介绍这两个功能的解决方案,因为这不是首先要问的问题。

I won't cover the solutions for these two features in here, because that isn't what was being asked in the first place.

希望这会有所帮助!

这篇关于如何从uri确定文件的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆