Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC upload file - HTTP Status 405 - Request method 'POST' not supported

I'm trying to upload file via JSP and controller but I always get

HTTP Status 405 - Request method 'POST' not supported

type Status report

message Request method 'POST' not supported

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

This is my form (only part of all 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 part of controller (only looged file name now):

@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 always I get:

HTTP Status 405 - Request method 'POST' not supported

Could you please help me someone? :(


My controller without all 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 run on:

http://localhost:8080/prosvaly/

I'm using in all for the same "action style" and it works. In this form when I clik on the button. It redirects me on the right way. I tried to change my action on

action="/prosvaly/admin/productfileadd

But still same error. And when I change 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 that problem is not in action, because GET method can find the same URL

like image 429
user1604064 Avatar asked Jul 26 '14 09:07

user1604064


1 Answers

The main problem was in spring security. I resolved this problem. Sprinf security blocks my URL but I don't know why.

I resolved this problem, I added ?${_csrf.parameterName}=${_csrf.token} to end of my form action

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

Now it works!

like image 66
user1604064 Avatar answered Oct 02 '22 00:10

user1604064