gpt4 book ai didi

Spring HTTP Status 400 - 客户端发送的请求在语法上不正确

转载 作者:行者123 更新时间:2023-12-02 10:19:49 26 4
gpt4 key购买 nike

我有一个表单,用户可以使用该表单将文件上传到本地驱动器,并将一些数据保存到数据库中。但是出现了这个错误消息,我想知道如何处理它。

   HTTP Status 400 - The request sent by the client was syntactically incorrect.

Controller

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

我尝试先保存表单,然后我将使用单独的方法发送它。

最佳答案

您的方法声明如下:

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

请参阅引用资料以了解更多详细信息

The Errors or BindingResult parameters have to follow the model object that is being bound immediately as the method signature might have more that one model object and Spring will create a separate BindingResult instance for each of them [...]

参见here

关于Spring HTTP Status 400 - 客户端发送的请求在语法上不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16399101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com