【发布时间】:2014-09-18 03:16:19
【问题描述】:
我正在尝试通过 JSP 和控制器上传文件,但我总是得到 p>
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
【问题讨论】:
-
@RequestMappings 中的点有特殊含义,如果您真的不需要它们,请尝试在没有它们的情况下执行操作,例如@RequestMapping(value = "/admin/productfileadd", method = RequestMethod.POST)和<form method="POST" enctype="multipart/form-data" action="productfileadd"> -
我试过没有点,我得到了同样的错误:(
-
你能查一下你发帖的地方吗?对于 Fiddler,也许您没有调用正确的操作,它应该是
action="/admin/productfileadd"。 -
该错误意味着 Spring MVC 无法为 POST 请求找到合适的控制器。发布您的完整控制器代码以及 URL 映射配置。
-
顺便说一句,只是为了澄清这与您的文件上传代码无关。您配置 Spring MVC 的方式本质上是错误的 - 不是特定于上传功能。
标签: java spring-mvc file-upload