Spring MVC upload file - HTTP Status 405 - Request

2019-06-16 10:12发布

问题:

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

回答1:

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!



回答2:

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

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



回答3:

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

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