上传后检查文件类型 [英] Check file type after upload
问题描述
我可以在页面上有多个文件上传框。哪些有不同的要求,应该接受不同类型的文件。
对于说: -
输入文件1应该只是exe
输入文件2应该只是html
输入文件3应该只用pptx
我可以检查扩展和检查是一个解决方案,但可能会发生一些可能伪造扩展名并上传 txt
文件 exe
等等
那么我怎样才能检查是否正确的文件类型上传或不使用java
有一些解决方案同样的东西,但我不确定如何做到这一点?对于commons-fileupload API,你可以得到如下所示的内容类型: $ b $
FileItem文件;
String contentType = file.getContentType();
基于此,您可以提供一些 if-else
来实现你的逻辑。 FileItem可以是 DefaultFileItem 。
I am uploading a file to server using normal upload API's available. Now i want to check whether uploaded file is valid file.
I can have multiple file upload box on page. Which have different requirements and should accept different type of files.
For say :-
Input file 1 should take just exe
Input file 2 should take just html
Input file 3 should take just pptx
i can check extension and check is one solution but it may happen some one may fake the extension and upload txt
file instead of exe
and so
So how can i check whether correct type of file has been uploaded or not using java
Got some Solution for the same stuff but am unsure how to do it? Any help pls
For commons-fileupload API, you can get the content type as following:
FileItem file;
String contentType = file.getContentType();
Based on that, you can provide some if-else
to implement your logic. The FileItem may be DefaultFileItem.
这篇关于上传后检查文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!