use*_*064 8 java file-upload spring-mvc
我试图通过JSP和控制器上传文件,但我总是得到
HTTP状态405 - 不支持请求方法"POST"
类型状态报告
消息请求方法'POST'不受支持
description对于请求的资源,不允许使用指定的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>
Run Code Online (Sandbox Code Playgroud)
我的控制器部分(现在只有一个文件名):
@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";
}
Run Code Online (Sandbox Code Playgroud)
和servlet-context.xml的一部分
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
Run Code Online (Sandbox Code Playgroud)
但总是得到:
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";
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序运行于:
http://localhost:8080/prosvaly/
Run Code Online (Sandbox Code Playgroud)
我正在使用所有相同的"动作风格",它的工作原理.当我按下按钮时,以这种形式.它以正确的方式重定向我.我试图改变我的行动
action="/prosvaly/admin/productfileadd
Run Code Online (Sandbox Code Playgroud)
但仍然是同样的错误.当我将方法类型从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
Run Code Online (Sandbox Code Playgroud)
所以我认为这个问题不起作用,因为GET方法可以找到相同的URL
use*_*064 12
主要问题是春季安全问题.我解决了这个问题.Sprinf安全阻止我的URL,但我不知道为什么.
我解决了这个问题,我添加了?$ {_ csrf.parameterName} = $ {_ csrf.token}来结束我的表单操作
<form method="POST" action="uploadOneFile**?${_csrf.parameterName}=${_csrf.token}**" enctype="multipart/form-data">
Run Code Online (Sandbox Code Playgroud)
现在它有效!
| 归档时间: |
|
| 查看次数: |
8740 次 |
| 最近记录: |