struts2无法检测文件的内容吗? (重命名扩展名问题) [英] Can't struts2 detect the content of a file? (Renaming extension trouble)

查看:96
本文介绍了struts2无法检测文件的内容吗? (重命名扩展名问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用struts2完成的应用程序.我们将上传的文件限制为Microsoft文档和acrobat pdf.一切都很好.但是,当用户更改文件的扩展名时,struts 2无法检测到该更改并接受文件.

We have an application done with struts2. We limited the uploaded files to microsoft documents and acrobat pdf. All go fine. But when a user change the extension of the file, struts 2 is unable to detect that change and accept the file.

例如logo.png-> logo.pdf

For example logo.png -> logo.pdf

我们在struts2文件中的配置如下:

Our configuration in the struts2 file is like this:

<interceptor-ref name="interceptorFileStack">
            <param name="fileUpload.allowedTypes">application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document</param>
            <param name="fileUpload.allowedExtensions">.pdf,.docx,.doc</param>
            <param name="fileUpload.maximumSize">4194304</param>
</interceptor-ref>

我以为allowedExtensions控制着扩展名,allowedTypes文件的内容...

I thought that allowedExtensions control the extension, and allowedTypes the content of the file...

是否仅通过struts 2可以检测到扩展名的变化?还是我需要另一个图书馆?有什么建议吗?

Anyway to detect that change of the extension only with struts 2? Or i need another library? Any recommendation?

推荐答案

在大多数情况下,浏览器发送的MIME类型都是由文件扩展名派生的.因此,重命名的jpg-> pdf是"application/pdf"文件类型.

Most of the time the MIME-Type sent by the browser is derived by the file extension. Thus a renamed jpg->pdf is a "application/pdf" filetype.

如果您不信任用户,必须确认正确的数据类型,则必须使用 Apache Tika JHOVE

If you can't trust your users and have to confirm the correct data type you have to use something like Apache Tika or JHOVE

提卡(Tika)的一个小例子是:

A small example for Tika would be:

Path path = Paths.get("myfile.txt");
TikaConfig tika = new TikaConfig();
Metadata metadata = new Metadata();
metadata.set(Metadata.RESOURCE_NAME_KEY, path.toString());
String mimetype = tika.getDetector().detect(TikaInputStream.get(path), metadata).toString();
System.out.println("File " + path + " is " + mimetype);

(摘自教程)

JHOVE主要是您可以使用的gui/命令行工具,但也应该可以通过API使用它.

JHOVE is mainly a gui/commandline tool you can use, but it should also be possible to use this via an API.

这篇关于struts2无法检测文件的内容吗? (重命名扩展名问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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