我是春天的新人。我已经开始制作一个Web应用程序来上传文件然后写入数据库。我在Netbeans中使用Spring MVC和Maven制作它。
我根据本教程制作了一个完全正常的基础项目
并尝试为我的应用程序扩展它,想要根据官方Spring教程制作文件上传组件
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-multipart
但它不起作用,出现错误:
HTTP状态400 - 必需的MultipartFile参数'file'不存在
我使用wyslij.jsp(上传文件的格式)扩展了这个项目
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Wysylanie pliku</title>
</head>
<body>
<form method="get" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
并为上传文件添加了一个名为UpladController.java的文件
package helloweb;
import java.io.IOException;
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
public class UploadController
{
@RequestMapping(value = "wyslij", method = RequestMethod.GET)
public String handleFormUpload(@RequestParam("file") MultipartFile file) throws IOException
{
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:tak";
} else
{
return "redirect:nie";
}
}
}
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- name of the project//-->
<display-name>HelloProject</display-name>
<servlet>
<servlet-name>front-controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>front-controller</servlet-name>
<url-pattern>/application/*</url-pattern>
</servlet-mapping>
<!-- max time of the session //-->
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!-- default page //-->
<welcome-file-list>
<welcome-file>application/wyslij.jsp</welcome-file>
</welcome-file-list>
</web-app>
和front-controller-serlvet.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- configuration to fetch jsp files automatically from WEB-INF/jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<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>
<context:component-scan base-package="helloweb" />
</beans>
有人能说出来,为什么会出现错误并解释?
编辑:
我决定使用教程中制作的form.jsp和HelloController.java并将其转换为文件上传(它们比我的代码工作更多)
form.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form Page</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="http://localhost:8084/Hello/application/form">
<label>file to send: <input type="file" name="file" /></label>
<input type="submit" />
</form>
</body>
</html>
和HelloController.java
package helloweb;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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
public class HelloController
{
@RequestMapping(value = "form", method = RequestMethod.POST)
public String login(@RequestParam(value = "file", required = true) MultipartFile file) throws IOException
{
if (!file.isEmpty())
{
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:tak";
}
else
{
return "redirect:nie";
}
}
@RequestMapping("form")
public String viewLoginPage(Model model)
{
return "form";
}
}
现在我至少在开始页面上正确显示了文件上传表单,但是在选择文件并单击按钮后,我得到了更多错误:
HTTP Status 500 - Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
答案 0 :(得分:0)
您正在发送带有需要Http POST的multipart / form数据编码的文件。在您的控制器中将其更改为如下所示。
@RequestMapping(value = "wyslij", method = RequestMethod.POST)
也在你的jsp中。
<form method="post" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
答案 1 :(得分:0)
Chnaged和Controller现在是:
@Controller
public class UploadController
{
@RequestMapping(value = "wyslij", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("file") MultipartFile file) throws IOException
{
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:tak";
} else
{
return "redirect:nie";
}
}
}
和jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Wysylanie pliku</title>
</head>
<body>
<form method="post" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
邮递员测试服务 GET测试的结果:
HTTP Status 405 - Request method 'GET' not supported
type Status report
message Request method 'GET' not supported
description The specified HTTP method is not allowed for the requested resource.
POST测试的结果:
HTTP Status 400 - Required MultipartFile parameter 'file' is not present
type Status report
message Required MultipartFile parameter 'file' is not present
description The request sent by the client was syntactically incorrect.