如何查看上传的文件是图片还是其他文件? [英] How to check a uploaded file whether it is an image or other file?

查看:390
本文介绍了如何查看上传的文件是图片还是其他文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,我有一个图像上传模块.我想检查上传的文件是图像文件还是其他文件.我在服务器端使用Java.

In my web application I have an image uploading module. I want to check the uploaded file whether it's an image file or any other file. I am using Java in server side.

该图像在Java中读为BufferedImage,然后用ImageIO.write()

The image is read as BufferedImage in java and then I am writing it to disk with ImageIO.write()

我应该如何检查BufferedImage,究竟是图像还是其他?

How shall I check the BufferedImage, whether it's really an image or something else?

任何建议或链接将不胜感激.

Any suggestions or links would be appreciated.

推荐答案

我假设您正在servlet上下文中运行它.如果可以仅根据文件扩展名检查内容类型,那么请使用

I'm assuming that you're running this in a servlet context. If it's affordable to check the content type based on just the file extension, then use ServletContext#getMimeType() to get the mime type (content type). Just check if it starts with image/.

String fileName = uploadedFile.getFileName();
String mimeType = getServletContext().getMimeType(fileName);
if (mimeType.startsWith("image/")) {
    // It's an image.
}

默认的mime类型在所讨论的servlet容器的web.xml中定义.例如在Tomcat中,它位于/conf/web.xml中.您可以在Webapp的/WEB-INF/web.xml中对其进行扩展/覆盖,如下所示:

The default mime types are definied in the web.xml of the servletcontainer in question. In for example Tomcat, it's located in /conf/web.xml. You can extend/override it in the /WEB-INF/web.xml of your webapp as follows:

<mime-mapping>
    <extension>svg</extension>
    <mime-type>image/svg+xml</mime-type>
</mime-mapping>

但这不会阻止您通过更改文件扩展名来欺骗您.如果您也想解决这个问题,那么您还可以根据 actual 文件内容确定mime类型.如果仅检查BMP,GIF,JPG或PNG类型(而不是TIF,PSD,SVG等)负担得起,则可以直接将其直接输入

But this doesn't prevent you from users who are fooling you by changing the file extension. If you'd like to cover this as well, then you can also determine the mime type based on the actual file content. If it's affordable to check for only BMP, GIF, JPG or PNG types (but not TIF, PSD, SVG, etc), then you can just feed it directly to ImageIO#read() and check if it doesn't throw an exception.

try (InputStream input = uploadedFile.getInputStream()) {
    try {
        ImageIO.read(input).toString();
        // It's an image (only BMP, GIF, JPG and PNG are recognized).
    } catch (Exception e) {
        // It's not an image.
    }
}

但是,如果您还想涵盖更多图像类型,请考虑使用第三方库,通过嗅探 JMimeMagic Apache Batik 支持SVG.下面的示例使用JMimeMagic:

But if you'd like to cover more image types as well, then consider using a 3rd party library which does all the work by sniffing the file headers. For example JMimeMagic or Apache Tika which support both BMP, GIF, JPG, PNG, TIF and PSD (but not SVG). Apache Batik supports SVG. Below example uses JMimeMagic:

try (InputStream input = uploadedFile.getInputStream()) {
    String mimeType = Magic.getMagicMatch(input, false).getMimeType();
    if (mimeType.startsWith("image/")) {
        // It's an image.
    } else {
        // It's not an image.
    }
}

如果有必要,您可以组合使用,并且胜过一个.

You could if necessary use combinations and outweigh the one and other.

也就是说,您不一定需要ImageIO#write()才能将上传的图像保存到磁盘.只需将获得的InputStream直接写到Path或任何OutputStream类似FileOutputStream的常规Java IO方法就绰绰有余了(另请参见

That said, you don't necessarily need ImageIO#write() to save the uploaded image to disk. Just writing the obtained InputStream directly to a Path or any OutputStream like FileOutputStream the usual Java IO way is more than sufficient (see also Recommended way to save uploaded files in a servlet application):

try (InputStream input = uploadedFile.getInputStream()) {
    Files.copy(input, new File(uploadFolder, fileName).toPath());
}

除非您想收集一些图像信息,例如其尺寸和/或要进行操作(裁剪/调整大小/旋转/转换/等),否则.

Unless you'd like to gather some image information like its dimensions and/or want to manipulate it (crop/resize/rotate/convert/etc) of course.

这篇关于如何查看上传的文件是图片还是其他文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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