HTTP status 500 when using file upload using freemarker

I am trying to add image loading to my freemarker page (ftl file) using spring and hibernate technology - this is the error I had every time I started the application:

HTTP Status 500 - The server encountered an internal error () that prevented it from completing this request.

and this is the code:

1-POM file:

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

2-app-config.xml:

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

3-ftl file:

<input type="file" id="image" name="image" value="">

4-web controller:

@RequestMapping(method= RequestMethod.POST)
public String post(Model model , HttpServletRequest req , HttpSession session,@RequestParam("image") MultipartFile multipartFile) throws IOException{

    // transfer the uploaded image to the place where images exist in project 
    multipartFile.getBytes();
    File destination = new File("/home/user/Pictures/" + multipartFile.getOriginalFilename());
    multipartFile.transferTo(destination);


    // delete the original uploaded image
    destination.delete();


    return "redirect:index";

}
+3
source share
1 answer

Be sure to follow the documentation for the letter. As a rule, you correctly defined your element <form>with its attribute enctype?

<form method="post" action="/form" enctype="multipart/form-data">

, . - , ; 500 ERROR, . !:)

+1

Source: https://habr.com/ru/post/1779986/


All Articles