Java 使用Spring MVC上载文件时使用HTTP 400
我在尝试上载文件时收到HTTP 400错误 以下是客户端代码: form.jspJava 使用Spring MVC上载文件时使用HTTP 400,java,html,spring,jsp,spring-mvc,Java,Html,Spring,Jsp,Spring Mvc,我在尝试上载文件时收到HTTP 400错误 以下是客户端代码: form.jsp <form method="POST" enctype="multipart/form-data" action="<c:url value="/upload"/>"> <table class="table"> <thead> <tr> <td></td&g
<form method="POST" enctype="multipart/form-data" action="<c:url value="/upload"/>">
<table class="table">
<thead>
<tr>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<tr>
<td>File to upload:</td>
<td><input type="file" name="file" /></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Upload" /></td>
</tr>
</tbody>
</table>
</form>
如果您需要更多信息,请发表评论。您还可以添加配置代码吗?请尝试此操作-
,然后再
@RequestMapping(method = RequestMethod.POST, value = "/upload")
public String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes)
{
if (name.contains("/"))
{
redirectAttributes.addFlashAttribute("message", "Folder separators not allowed");
return "redirect:upload";
}
if (name.contains("/"))
{
redirectAttributes.addFlashAttribute("message", "Relative pathnames not allowed");
return "redirect:upload";
}
if (!file.isEmpty())
{
try
{
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(Application.ROOT + "/" + name)));
FileCopyUtils.copy(file.getInputStream(), stream);
stream.close();
redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + name + "!");
} catch (Exception e)
{
redirectAttributes.addFlashAttribute("message",
"You failed to upload " + name + " => " + e.getMessage());
}
} else
{
redirectAttributes.addFlashAttribute("message",
"You failed to upload " + name + " because the file was empty");
}
return "redirect:upload";
}