Spring MVC上传文件-HTTP状态405-不支持请求方法“ POST”


问题内容

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

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

类型状态报告

消息不支持请求方法“ POST”

说明所请求的资源不允许使用指定的HTTP方法。

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

<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>

我的控制器部分(现在仅清除了文件名):

@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";
}

以及servlet-context.xml的一部分

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

但是我总是得到:

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

你能帮我一个人吗?:(


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

@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";
    }


}

我的应用程序运行在:

http://localhost:8080/prosvaly/

我使用的都是相同的“动作样式”,并且可以正常工作。以这种形式单击按钮时。它以正确的方式重定向了我。我试图改变对

action="/prosvaly/admin/productfileadd

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

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

所以我认为这个问题没有解决,因为GET方法可以找到相同的URL


问题答案:

主要问题是spring安全。我解决了这个问题。Sprinf安全性阻止了我的URL,但我不知道为什么。

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

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

现在可以了!