【问题标题】:SpringMVC-FileUpload - HTTP Status 400 -The request sent by the client was syntactically incorrectSpringMVC-FileUpload - HTTP 状态 400 - 客户端发送的请求在语法上不正确
【发布时间】:2017-01-22 09:06:19
【问题描述】:

我是 spring 新手并尝试使用 spring 4.2.7commons-io-1.3.2commons-fileupload-1.3 上传图片,jdk 1.8

但不幸的是,我收到了错误,即 HTTP 状态 400 - 客户端发送的请求在语法上不正确。

请帮忙。

代码sn-p是

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>

控制器类是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>   

【问题讨论】:

    标签: spring spring-mvc file-upload


    【解决方案1】:

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

    我没有尝试运行您的代码,但您是否尝试过从 FormExample 中删除、重命名 profilePhoto 及其 getter 和 setter,或者将其类型设置为 MultipartFile?

    我怀疑尽管您的控制器方法具有同名的参数,但 Spring 可能会尝试将参数值分配给方法参数和表单属性,并且在分配给时未能将 MultipartFile 转换为 java.sql.Blob表单属性。

    【讨论】:

    • 谢谢@XDF,我在 formExamplePage.jsp 中添加了 enctype="multipart/form-data" 并将文件名修改为不同的名称,即 并相应地修改了控制器类。现在它完美地工作了。
    猜你喜欢
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 2013-12-09
    相关资源
    最近更新 更多