Spring MVC Download File - HTTP Status 405 - "POST" Request Method Not Supported

I try to upload a file via JSP and controller, but I always get

HTTP Status 405 - The POST request method is not supported.

enter status report

message "POST" request method is not supported

description The specified HTTP method is not allowed for the requested resource.

This is my form (only part of the entire JSP page):

<form method="POST" enctype="multipart/form-data" action="product.file.add"> <input name="productId" type="hidden" /> <tr> <th>Foto: </th> <td><input type="file" name="file" /></td> </tr> <tr> <td class="bt" ><input type="submit" value="Add image" /></td> <td class="bt" ><input type="submit" value="Continue without image" /></td> </tr> </form> 

My controller part (now only the looged file name):

 @RequestMapping(value = "/admin/product.file.add", method = RequestMethod.POST) public String productFileUpload(@RequestParam("file") MultipartFile file, @RequestParam("productId") int productId) { logger.info(file.getName()); return "redirect:/admin/product"; } 

And part of servlet-context.xml

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

But I always get:

HTTP Status 405 - The POST request method is not supported.

Could you help me?: (


My controller without any method:

 @Controller public class ProductController { @Autowired private ProductDao productDao; @Autowired private ProducerDao producerDao; @Autowired private SectionDao sectionDao; @Autowired private TasteDao tasteDao; @Autowired private CategoryDao categoryDao; private static final Logger logger = LoggerFactory .getLogger(ProductController.class); @RequestMapping(value = "/admin/productfileadd", method = RequestMethod.POST) public String productFileUpload(@RequestParam("file") MultipartFile file, @RequestParam("productId") int productId) { logger.info(file.getName()); return "redirect:/admin/product"; } } 

My application is running:

 http://localhost:8080/prosvaly/ 

I use everything for the same "action style" and it works. In this form, when I click on the button. He redirects me to the right path. I tried to change my action to

 action="/prosvaly/admin/productfileadd 

But still the same mistake. And when I change the method type from POST to GET, I get another error:

 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: The current request is not a multipart request 

So, I think the problem is not in action, because the GET method can find the same URL

+7
java spring-mvc file-upload
source share
3 answers

The main concern was spring security. I solved this problem. Sprinf protection blocks my URL, but I don’t know why.

I solved this problem, I added ? $ {_ csrf.parameterName} = $ {_ csrf.token} to complete the form

 <form method="POST" action="uploadOneFile**?${_csrf.parameterName}=${_csrf.token}**" enctype="multipart/form-data"> 

Now it works!

+10
source

the value of @RequestMapping is "/admin/product.file.add" and the form action is action = "product.file.add". I think there should be action = "/admin/product.file.add"

Or you can try using <form method="POST" enctype="multipart/form-data" action="/product.file.add">

0
source

I had the same problem and my solution was different. In my case, I use annotation based configurations. I annotated my class using @Controller , but I never told the configuration class to scan my package where the controllers were, and the way to do this is to annotate your configuration class with

 @ComponentScan(basePackages = "com.controllers.location.package") 
0
source

All Articles