2016-09-14 114 views
0

我是新来的春天,并试图使用春天4.2.7公地IO-1.3.2上传图片,commons-fileupload-1.3jdk 1.8用SpringMVC-的FileUpload - HTTP状态400通过客户端发送请求-The是语法不正确

但不幸的是我收到错误,例如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; 
    } 
} 

弹簧配置文件是

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

回答

1

你见过this question on multipartFiles and blobs?您的表单:表单可能需要enctype =“multipart/form-data”。

我还没有试图运行你的代码,但你有没有尝试从FormExample中删除,重命名profilePhoto及其getter和setter,或将其类型设置为MultipartFile?

我怀疑,尽管你的控制器方法有一个参数具有相同的名称Spring可能会试图将参数值分配给方法参数和表单属性,并且无法在分配时将MultipartFile转换为java.sql.Blob到表单属性。

+0

谢谢@XDF,我在formExamplePage.jsp中添加了enctype =“multipart/form-data”,并将文件名修改为不同的内容,即​​ 并相应地修改控制器类。现在它完美地工作。 – NagenSahu

相关问题