Java – how to limit the file types uploaded to spring mvc3 controller

I am using spring MVC 3 to handle the file upload of my web application Now, I can limit the size of the file being uploaded using the following configuration defined in my XML context file:

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

I have browsed how the Internet limits file types, but it has no effect Most of the articles I found only teach how to limit file size, not file type Thank you in advance for your help

Solution

Attempt to perform check / route in the 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";
        }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>