猿问

确定图片网址的文件扩展名

是否有可靠,快速的方法来确定图像URL的文件扩展名?这些是我看到的一些选项,但是对于以下格式的图像,它们都不能始终如一地工作

https://cdn-image.blay.com/sites/default/files/styles/1600x1000/public/images/12.jpg?itok=e-zA1T

我试过了:

new MimetypesFileTypeMap().getContentType(url)

在通用的“ application / octet-stream”中产生结果,在这种情况下,我使用以下两种方法:

Files.getFileExtension
FilenameUtils.getExtension

我想在可能的情况下避免使用正则表达式,因此是否有另一个实用程序可以正常获取具有args(.jpeg?blahblah)的链接。我也想避免以任何方式下载图像或与url的连接,因为这应该是一个高性能调用


杨__羊羊
浏览 165回答 2
2回答

弑天下

如果您可以确信这些网址没有格式错误,该如何处理:FilenameUtils.getExtension(URI.create(url).getPath())

MM们

难道您只是看URL中的文件扩展名?所以会是这样的:public static String getFileExtension(String url) {&nbsp; &nbsp; int phpChar = url.length();&nbsp; &nbsp; for(int i = 0; i < url.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(url.charAt(i) == '?') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; phpChar = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; int character = phpChar - 1;&nbsp; &nbsp; while(url.charAt(character) != '.') character -= 1;&nbsp; &nbsp; return url.substring(character + 1, phpChar);}也许不是最优雅的解决方案,但是即使使用php,它也可以工作吗?在网址中。
随时随地看视频慕课网APP

相关分类

Java
我要回答