我正在尝试通过Spring MVC上传文件,并且我一直在获取HTTP 400 - 客户端发送的请求在语法上是不正确的错误。以下是我的设置:
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>FileUploader</display-name>
<servlet>
<servlet-name>FileUploader</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploader</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
FileUploader-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.fileuploader"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/JSP/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
FileUploadController.java
package com.fileuploader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/files")
public class FileUploaderController {
@RequestMapping(value = "/selectFile", method = RequestMethod.GET)
public String selectFile()
{
return "selectFile";
}
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFile(@RequestParam("file") MultipartFile fileUpload)
{
//System.out.println(file.getOriginalFilename());
return "uploadSuccessful";
}
}
两个JSP视图, selectFile.jsp 和 uploadSuccessful.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload File</title>
</head>
<body>
<h1>Upload Your File Here</h1>
<form action="/FileUploader/files/uploadFile" method="POST" enctype="multipart/form-data">
<table>
<tr>
<td>Select a file to upload : <input type="file" name="file"/></td>
</tr>
<tr>
<td><input type="submit" value="Upload"/></td>
</tr>
</table>
</form>
</body>
</html>
一
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Upload Successful</title>
</head>
<body>
<h1>Upload Successful</h1>
</body>
</html>
可能导致此错误的原因是什么?谢谢你的帮助。
答案 0 :(得分:0)
我需要将以下配置添加到FileUploader-servlet.xml
:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />