我有一个表单,用户可以使用该表单将文件上传到本地驱动器,并将一些数据保存到数据库中。但是这个错误信息出现了,我想知道如何处理它。
HTTP Status 400 - The request sent by the client was syntactically incorrect.
控制器
@RequestMapping(value = "/main/user/setter/addpage", method =
RequestMethod.POST, params = "save")
public String saveProcess(@ModelAttribute("module") Module module,
@RequestParam("userId") Integer userId,
@RequestParam("name") String name,
@RequestParam("file") MultipartFile file,
BindingResult result, HttpSession session) {
if (result.hasErrors()) {
return "redirect:/main/user/setter/settingpage";
}
else
if(module != null){
try {
MultipartFile filea = module.getFileData();
InputStream inputStream = null;
OutputStream outputStream = null;
if (filea.getSize() > 0) {
inputStream = filea.getInputStream();
outputStream = new FileOutputStream("C:\\Test\\"
+ filea.getOriginalFilename());
System.out.println("Uploaded File Name");
System.out.println(filea.getOriginalFilename());
int readBytes = 0;
byte[] buffer = new byte[8192];
while ((readBytes = inputStream.read(buffer, 0, 8192)) !=
-1) {
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
session.setAttribute("success", "File Uploaded");
session.setAttribute("uploadFile", "C:\\Test\\"
+ filea.getOriginalFilename());
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Delegate to service
moduleService.add(userId, module);
return "redirect:/main/user/setter/settingpage";
}
表单本身
<c:url var="processUrl" value="/main/user/setter/addpage?userId=2" />
<form:form modelAttribute="module" method="POST" action="${processUrl}" name="module"
enctype="multipart/form-data">
<form:label path="fileName">Document Title:</form:label>
<form:input path="fileName" name="name"/><br/>
<form:label path="documentPath">Coursework Sample:</form:label>
<form:input path="documentPath" type="file" name="file" id="file" size="43.9"/><br/>
<form:label path="liveDate">Live Date:</form:label>
<form:input path="liveDate"/><br/>
<input type="submit" name="save" value="Save" id="save"/>
<input type="submit" name="send" value="Send" id="send"/>
</form:form>
我正在尝试保存表单,我将使用单独的方法发送它。
答案 0 :(得分:9)
您的方法声明如下:
public String saveProcess(@ModelAttribute("module") Module module,
@RequestParam("userId") Integer userId,
@RequestParam("name") String name,
@RequestParam("file") MultipartFile file,
BindingResult result, HttpSession session) ...
假设您尝试将BindingResult
应用于Module
实例,则需要更改方法定义,以便在BindingResult
实例之后找到Module
public String saveProcess(@ModelAttribute("module") Module module,
BindingResult result,
@RequestParam("userId") Integer userId,
@RequestParam("name") String name,
@RequestParam("file") MultipartFile file,
HttpSession session) ...
有关详细信息,请参阅参考资料
错误或BindingResult参数必须遵循模型对象 正如方法签名可能具有的那样立即绑定 更多的是一个模型对象和Spring将创建一个单独的 每个[...]
的BindingResult实例
见here。
答案 1 :(得分:0)
错误HTTP Status 400 - The request sent by the client was syntactically incorrect.
可能来自上下文中缺少的multipartResolver
(servlet-context.xml
)
`<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />`
有关详情,请参阅SpringMVC-FileUpload - The request sent by the client was syntactically incorrect
的回复