Spring MVC上传文件 - HTTP状态405 - 不支持请求方法“POST” [英] Spring MVC upload file - HTTP Status 405 - Request method 'POST' not supported

查看:750
本文介绍了Spring MVC上传文件 - HTTP状态405 - 不支持请求方法“POST”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过JSP和控制器上传文件,但我总是得到



HTTP状态405 - 请求方法'POST'不受支持



类型状态报告

消息请求方法POST不支持

描述指定的HTTP方法不允许被请求的资源。

这是我的表单(只是所有JSP页面的一部分):

 < form method =POSTenctype =multipart / form-dataaction =product.file.add> 
< input name =productIdtype =hidden/>
< tr>
< th> Foto:< / th>
< td>< input type =filename =file/>< / td>
< / tr>
< tr>
< td class =bt>< input type =submitvalue =Add image/>< / td>
< td class =bt>< input type =submitvalue =Continue without image/>< / td>
< / tr>
< / form>

我的控制器部分(现在只有looged文件名):

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

部分servlet-context.xml

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

但总是得到:

HTTP状态405 - 不支持请求方法'POST'

你能帮我一个人吗? :




我的控制器没有所有的方法:

<$ p $ @Controller
public class ProductController {

@Autowired
Private ProductDao productDao;

@Autowired
private ProducerDao producerDao;

@Autowired
private SectionDao sectionDao;
$ b @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());
returnredirect:/ admin / product;
}



我的应用程序运行在:

  http:// localhost:8080 / prosvaly / 

我使用所有相同的动作风格,它的工作原理。在这个表格中,当我按下按钮时。它以正确的方式重定向我我试图改变我的行动

  action =/ prosvaly / admin / productfileadd 

但是同样的错误,而且当我将方法类型从POST更改为GET时,出现另一个错误:

  org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为org.springframework.web.multipart.MultipartException:当前请求不是多部分请求



所以我觉得这个问题不在行动中,因为GET方法可以找到相同的URL

解决方案

主要问题是在spring security中,我解决了这个问题,sprinf安全阻止了我的URL,但我不知道为什么。 >

我解决了这个问题,我添加了?$ {_ csrf.parameterName} = $ {_ csrf.token} 来结束我的表单操作


$ b $

 < form method =POSTaction =uploadOneFile **?$ {_ csrf.parameterName} = $ {_ csrf.token} ** enctype =mul tipart /格式数据> 

现在可以使用了!

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

解决方案

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!

这篇关于Spring MVC上传文件 - HTTP状态405 - 不支持请求方法“POST”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆