Java HTTP 400-向我的Spring MVC控制器添加参数时的状态
我使用的是Spring MVC,其表单如下所示: 表单上有一个用于上传图片的按钮,当我尝试使用参数将文件放入控制器时,当我按下ok按钮时,会得到HTTP-400状态 问题是什么?如何用文件解决此问题 我的jsp代码:Java HTTP 400-向我的Spring MVC控制器添加参数时的状态,java,spring,hibernate,spring-mvc,blob,Java,Spring,Hibernate,Spring Mvc,Blob,我使用的是Spring MVC,其表单如下所示: 表单上有一个用于上传图片的按钮,当我尝试使用参数将文件放入控制器时,当我按下ok按钮时,会得到HTTP-400状态 问题是什么?如何用文件解决此问题 我的jsp代码: <form:form method="post" commandName="editPersonBean" enctype="multipart/form-data"> <form:hidden path="id" /> &
<form:form method="post" commandName="editPersonBean" enctype="multipart/form-data">
<form:hidden path="id" />
<table class="myTable">
<c:if test="${editPersonBean.id > 0}">
<tr>
<th>ID</th>
<td>${editPersonBean.id}</td>
</tr>
</c:if>
<tr>
<td>Förnamn</td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td>Efternamn</td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td>Telefon nummer</td>
<td><form:input path="phoneNumber" /></td>
</tr>
<tr>
<td>E-Mail</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>Övrigt</td>
<td><form:input path="otherInfo" /></td>
</tr>
<tr>
<td>Bild</td>
<td><form:label path ="image" />
<input type="file" name="file" />
</td>
</tr>
<br>
<br>
<tr>
<td><c:set var="submitText">
OK
</c:set> <input type="submit" size="20" value="${submitText}" /> <a
href="<%=request.getContextPath()%>/person.html"></a></td>
<td></td>
</tr>
</table>
</form:form>
我遇到问题的控制器如下所示:
@Controller
@RequestMapping("/editPerson/{id}.html")
public class EditPersonContoller {
@Autowired
PersonService service;
@RequestMapping(method = RequestMethod.GET)
public ModelAndView index(@PathVariable int id) {
EditPersonBean bean = new EditPersonBean();
if (id > 0) {
Person person = service.getPerson(id);
bean.copyValuesToBean(person);
}
ModelAndView mav = new ModelAndView("editPerson");
mav.addObject("editPersonBean", bean);
return mav;
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView handleSubmit(EditPersonBean bean, BindingResult errors, @RequestParam("file") MultipartFile file) {
if (errors.hasErrors()) {
ModelAndView mav = new ModelAndView("editPerson");
mav.addObject("editPersonBean", bean);
return mav;
}
if (bean.getId() > 0) {
Person person = service.getPerson((int) bean.getId());
bean.copyBeanValuesToPerson(person);
saveImage(person, file);
service.updatePerson(person);
} else {
Person person = new Person();
bean.copyBeanValuesToPerson(person);
saveImage(person, file);
service.createPerson(person);
}
return new ModelAndView("redirect:/person.html");
}
public void saveImage(Person person, @RequestParam("file") MultipartFile file) {
try {
Blob blob = Hibernate.createBlob(file.getInputStream());
person.setImage(blob);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我还尝试使用bean保存图片:
公共类EditPersonBean{
private int id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private String otherInfo;
private Blob image;
public void copyValuesToBean(Person person){
setId((int) person.getId());
setFirstName(person.getFirstName());
setLastName(person.getLastName());
setEmail(person.getEmail());
setPhoneNumber(person.getPhoneNumber());
setOtherInfo(person.getOtherInfo());
setImage(person.getImage());
}
public void copyBeanValuesToPerson(Person person){
person.setId((int) getId());
person.setFirstName(getFirstName());
person.setLastName(getLastName());
person.setEmail(getEmail());
person.setPhoneNumber(getPhoneNumber());
person.setOtherInfo(getOtherInfo());
person.setImage(image);
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getOtherInfo() {
return otherInfo;
}
public void setOtherInfo(String otherInfo) {
this.otherInfo = otherInfo;
}
public Blob getImage() {
return image;
}
public void setImage(Blob image) {
this.image = image;
}
}
我正试图通过一个只调用存储库的服务将其保存到数据库中:
@Override
public void createPerson(Person person) {
session.getCurrentSession().save(person);
}
最后,它应该深入到模型并将其保存到数据库中:
@Column(name = "image")
@Lob
private Blob image;
但是没有图像被保存到数据库中,而是页面返回http:400-Status
这里有什么问题?如何在JSP上传的控制器中获取文件,将下面的方法放入控制器并调试代码
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
FileUpload file = (FileUpload)command;
MultipartFile multipartFile = file.getFile();
String fileName="";
if(multipartFile!=null){
fileName = multipartFile.getOriginalFilename();
//do whatever you want
}
System.out.println("filename :" + fileName);
return new ModelAndView("FileUploadSuccess","fileName",fileName);
}
说明了其对多部分文件上载的支持
基本上,您需要在ApplicationContext
中提供一个MultipartResolver
,它将可用于DispatcherServlet
。你有几个选择。一个常见的是commons多部分解析器
,您需要Apache的commons fileupload.jar
。文档提供了以下示例
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
DispatcherServlet
将在您的ApplicationContext
中检测此bean,并将其注册,以便它可以处理内容类型为多部分/…
的请求。在每个这样的请求中,它都会将HttpServletRequest
包装在multipathttpservletrequest
中
Spring使用RequestParamMethodArgumentResolver
解析带@RequestParam
注释的处理程序方法中带@RequestParam
注释的参数的参数
如果参数的类型也是MultipartFile
,Spring将HttpServletRequest
转换为先前创建的multipartttpservletrequest
,并使用它检索零件,其中一个是您的文件
如果未完成此配置,整个过程将失败,您可能会收到400个错误的请求。您的上下文中是否有多部分解析器
bean?否,如何配置?Sembrano您是否可以在GITHUB或任何存储库上共享完整的代码以便于调试?确保:是的,先生,它无法强制转换文件“file.getFile();”@Sembrano请在模型包com.abc.model中创建此模型;导入org.springframework.web.multipart.MultipartFile;公共类文件上传{MultipartFile file;//getter and setter methods}@Sembrano如果这个解决方案适合您,请右键单击我的答案,我的朋友,因此,它对其他人将是有用的。@Sotirios Delimanolis他将能够调试,即他是否能够获得上传的文件,或者不获取您提供的方法,该方法将不会注册为处理程序。它是完全随机的,不会被调用。@Sembrano让我们看看您更新的上下文配置(也可以添加您的web.xml)。@Sembrano还有,您的表单似乎没有提交到正确的位置或使用正确的请求方法。我没有看到POST
@Sembrano你有一个处理程序,但实际上没有任何东西在发送帖子(不是从你展示给我们的内容)。@Sembrano打开浏览器的网络控制台(如果你不知道那是什么,用谷歌搜索它)并执行提交。检查请求标题和正文,查看您是否提交了自己认为是的内容。然后检查是否有适当的处理程序方法。
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>