java 如何限制上传到 Spring MVC3 控制器的文件类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14155254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:16:11  来源:igfitidea点击:

How to restrict file types being uploaded to Spring MVC3 Controller

javaspringspring-mvcfile-upload

提问by woraphol.j

I am using Spring MVC3 to handle file upload for my web application. For now, I can restrict the file size being uploaded using the following configuration defined in my xml context file:

我正在使用 Spring MVC3 来处理我的 Web 应用程序的文件上传。现在,我可以使用在我的 xml 上下文文件中定义的以下配置来限制正在上传的文件大小:

<beans:bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <beans:property name="maxUploadSize" value="200000"/> 
</beans:bean>

I have scoured the web for how to restrict the file type but to no avail. Most of the articles I found only teach how to restrict the file size not the file type. Thank in advance for your help.

我已经在网上搜索如何限制文件类型,但无济于事。我发现的大多数文章只教如何限制文件大小而不是文件类型。预先感谢您的帮助。

回答by Kevin Bowersox

Try performing the check/routing in your controller's request handler method:

尝试在控制器的请求处理程序方法中执行检查/路由:

@RequestMapping("/save")
public String saveSkill(@RequestParam(value = "file", required = false) MultipartFile file) {   
        if(!file.getContentType().equalsIgnoreCase("text/html")){
            return "normalProcessing";
        }else{
            return "redirect: /some/page";
        }
}

回答by Avinash T.

You restrict file uploading by file types, you can extend org.springframework.web.multipart.commons.CommonsMultipartResolverclass. And add method to check file content-type or file type using MultipartFile.

您按文件类型限制文件上传,您可以扩展org.springframework.web.multipart.commons.CommonsMultipartResolver类。并添加方法来检查文件内容类型或使用MultipartFile.

Provide file types , those you want to restrict in configuration like -

提供文件类型,您希望在配置中限制的类型,例如 -

 <beans:bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <beans:property name="maxUploadSize" value="200000"/> 
    <beans:property name="restrictFileTypes" value="html,pdf,..."/> 
</beans:bean>

回答by Narendra

You can create a user defined method to do this check:

您可以创建一个用户定义的方法来执行此检查:

String fileExtentions = ".exe,.dmg,.mp3,.jar";
fileName = multipartFile.getOriginalFilename();
int lastIndex = fileName.lastIndexOf('.');
String substring = fileName.substring(lastIndex, fileName.length());

And check the condition:

并检查条件:

if (!fileExtentions.contains(substring))
    //logic
else
    //logic

回答by Vipul Jain

You can also check the Mime Type and according to that you can restrict the user to upload the files JMimeMagic Library will be used here.

您还可以检查 Mime 类型,据此您可以限制用户上传文件,这里将使用 JMimeMagic 库。

MagicMatch match = Magic.getMagicMatch(file.getBytes());
        System.out.println(match.getMimeType());