首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring MVC -上载文件状态400 -所需的文件参数‘MultipartFile’不存在

Spring MVC -上载文件状态400 -所需的文件参数‘MultipartFile’不存在
EN

Stack Overflow用户
提问于 2016-08-30 02:41:19
回答 2查看 1.8K关注 0票数 1

我是新来的春天。我有练习,使一个网络应用程序的文件被上传,然后写入数据库。我在Netbeans中使用Spring MVC和Maven。

基于本教程,我制作了一个完全可用的basic项目

https://saarapakarinen.wordpress.com/2015/01/11/building-a-basic-spring-3-web-project-netbeans-and-maven/

并尝试为我的应用程序扩展它,想要制作基于官方Spring教程的文件上传组件

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-multipart

但它不工作,得到一个错误:

HTTP状态400 -所需的文件参数‘MultipartFile’不存在

我已经用wyslij.jsp (上传文件的表单)扩展了这个项目

代码语言:javascript
复制
<%@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的控制器

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
<?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文件:

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<%@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

代码语言:javascript
复制
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";
    }
}

现在我至少在起始页正确显示了文件上传表单,但在选择一个文件并单击按钮后,我得到了一个错误:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2016-08-30 17:52:05

Chnaged和Controller现在是:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
<%@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测试的邮递员结果测试的服务:

代码语言:javascript
复制
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.

开机自检结果:

代码语言:javascript
复制
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.
票数 1
EN

Stack Overflow用户

发布于 2016-08-30 11:24:55

您正在发送多部分/表单数据编码的文件,这需要Http POST。在您的控制器中,将其更改为post,如下所示。

代码语言:javascript
复制
@RequestMapping(value = "wyslij", method = RequestMethod.POST)

在您的jsp中也是如此。

代码语言:javascript
复制
 <form method="post" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data">
        <input type="file" name="file"/>
        <input type="submit"/>
    </form>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39213024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档