我是初次尝试使用 spring 4.2.7 和 commons-io-1.3.2 , commons-fileupload-1.3上传图片, jdk 1.8 。
但不幸的是我收到错误,即HTTP状态400 - 客户端发送的请求在语法上是不正确的。
请帮忙。
代码段是
formExamplePage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring Form Example</title>
</head>
<body>
<h2>Form Example</h2>
<form:form commandName="formExample" action="formExampleDetails" method="post">
<table>
<tr>
<td>
<label>User Name:</label>
</td>
<td>
<form:input path="userName" placeholder="User Name"></form:input>
</td>
</tr>
<tr>
<td>
<label>Salary:</label>
</td>
<td>
<form:input path="salary" placeholder="salary in decimal"/>
</td>
</tr>
<tr>
<td>
<label>Gender</label>
</td>
<td>
<form:radiobutton path="gender" value="M" label="Male"/>
<form:radiobutton path="gender" value="F" label="Female"/>
</td>
</tr>
<tr>
<td>
<label>Profile Photo:</label>
</td>
<td>
<input name="profilePhoto" type="file"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit"/>
</td>
<td>
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form:form>
</body>
</html>
Controller类是ApplicationController.java
@Controller
public class ApplicationController {
@RequestMapping(value="/formExampleDetails",method=RequestMethod.POST)
public String formExampleDetails(@ModelAttribute FormExample formExample,
@RequestParam("profilePhoto") MultipartFile profilePhoto,ModelMap model){
System.out.println("User Name====>"+formExample.getUserName());
System.out.println("BirthDate====>"+formExample.getBirthDate());
System.out.println("Gender=======>"+formExample.getGender());
System.out.println("Salary=======>"+formExample.getSalary());
System.out.println("ProfilePhoto=>"+profilePhoto.getOriginalFilename());
return "index";
}
}
Pojo类,即FormExample.java是
package com.spring.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Date;
public class FormExample implements Serializable{
private static final long serialVersionUID = 5527691555730303451L;
private String userName;
private Date birthDate;
private BigDecimal salary;
private Blob profilePhoto;
private Character gender;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public Blob getProfilePhoto() {
return profilePhoto;
}
public void setProfilePhoto(Blob profilePhoto) {
this.profilePhoto = profilePhoto;
}
public Character getGender() {
return gender;
}
public void setGender(Character gender) {
this.gender = gender;
}
}
spring配置文件是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.spring"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="./"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000000"></property>
</bean>
</beans>
答案 0 :(得分:1)
你见过this question on multipartFiles and blobs吗?您的表单:表单可能需要enctype =&#34; multipart / form-data&#34;
我还没有尝试过运行您的代码,但是您是否尝试从FormExample中删除,重命名profilePhoto及其getter和setter,或者将其类型设置为MultipartFile?
我怀疑尽管你的控制器方法有一个具有相同名称的参数,但Spring可能会尝试将参数值分配给method参数和form属性,并且在分配时无法将MultipartFile转换为java.sql.Blob表单属性。