File uploading not working in spring mvc using spring form

后端 未结 1 638
孤独总比滥情好
孤独总比滥情好 2021-01-27 08:46

I have a jsp page in which i have used spring form tag so that i can use model attribute and bind data to it. It was working fine on submit the form but after adding encty

相关标签:
1条回答
  • 2021-01-27 09:49

    On its own, DispatcherServlet doesn't know how to handle multipart form data; that's why we require multipart resolver.

    You should register it in your servlet-config:

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

    Use CommonsMultipartFile or MultipartFile instead of File in your User object:

    private CommonsMultipartFile imageFile;
    

    You can try this code to save file:

    File imageFileSave = new File(Constants.MEDIA_FILE_PATH);
    FileUtils.writeByteArrayToFile(imageFileSave , imageFile.getBytes());
    
    0 讨论(0)
提交回复
热议问题