上传后检查文件类型

编程入门 行业动态 更新时间:2024-10-22 02:41:12
本文介绍了上传后检查文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在上传一个文件到服务器使用普通的上传API的可用。现在我想检查上传的文件是否是有效的文件。

我可以在页面上有多个文件上传框。哪些有不同的要求,应该接受不同类型的文件。

对于说: -

输入文件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.

更多推荐

上传后检查文件类型

本文发布于:2023-07-17 03:32:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件类型   上传

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!