当前位置: 首页 > 知识库问答 >
问题:

当前请求不是多部分请求-Spring MVC

潘涵煦
2023-03-14
  • Spring3.1。1.发布

我想上传两个文件的html格式,但我总是得到一个多部分请求异常。我在谷歌上搜索了很多,但没有找到正确的解决方案。

org.springframework.web.multipart.MultipartException: The current request is not a multipart request
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.assertIsMultipartRequest(RequestParamMethodArgumentResolver.java:184)
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:149)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:83)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:534)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:476)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:517)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:935)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:404)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:184)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:870)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:346)
at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:596)
at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1068)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:807)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:220)
at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:426)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:520)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:528)
at java.lang.Thread.run(Thread.java:680)

我的控制器:

        @RequestMapping(value = "blub", method = RequestMethod.POST)
    public String String(@RequestParam("version") String version, @RequestParam("changeLog") String changeLog,
            @RequestParam("status") String status, @RequestParam("y") MultipartFile y,
            @RequestParam("x") MultipartFile x, @RequestParam("zId") String zId, RedirectAttributes attr) {

        double size = 0.0;
        byte[] xFile = null;
        byte[] yFile = null;
        String downloadUrl = "blub";

        if (!x.isEmpty() && !y.isEmpty()) {
            try {
                xFile = x.getBytes();
                yFile = y.getBytes();
                size = x.getSize() / (1024 * 1024);
            } catch (IOException e) {
            }
            System.out.println("file upload received! Name:[" + version + "] Size:[" + xFile.length + "]");
            System.out.println("file upload received! Name:[" + version + "] Size:[" + yFile.length + "]");
        } else {
            return "file upload failed!";
        }

        zService.saveZ(new Z(changeLog, status, downloadUrl, size, new Date(), yFile, xFile,
                Integer.parseInt(zId)));
        return "redirect:Z";
    }

我的. jsp页面:

    <form method="POST" action="/form/blub" id="Z" class="form-horizontal" enctype=”multipart/form-data”>
                        <fieldset>
                            <legend>Create Z</legend>

                            <div class="control-group">
                                <label class="control-label" for="input">Version</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" id="version" name="version">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="textarea">Changelog</label>
                                <div class="controls">
                                    <textarea class="input-xlarge" id="textarea" rows="4" id="changeLog" name="changeLog"></textarea>
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input">Status</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" id="status" name="status">
                                </div>
                            </div>


                            <div class="control-group">
                                <label class="control-label" for="select01">Select list</label>
                                <div class="controls">
                                    <select id="Z" name="Z">

                                        <c:forEach items="${z}" var="current">
                                            <option value="${z.id}">
                                                <c:out value="${z.name}" />
                                            </option>
                                        </c:forEach>

                                    </select>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="x">File input</label>
                                <div class="controls">
                                    <input class="input-file" id="x" type="file">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="y">File input</label>
                                <div class="controls">
                                    <input class="input-file" id="y" type="file">
                                </div>
                            </div>

                            <div class="form-actions">
                                <button type="submit" class="btn btn-success">Create Z</button>
                                <button type="reset" class="btn">Cancel</button>
                            </div>
                        </fieldset>
                    </form>

spring-serverlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:annotation-config />
    <!-- use this for Spring Jackson JSON support -->
    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <tx:annotation-driven transaction-manager="transactionManager" />
    <jdbc:embedded-database id="dataSource" type="H2" />

    <context:component-scan base-package="com.x.y.z.config" />

    <context:component-scan base-package="com.x.y.z.market.dao" />

    <context:component-scan base-package="com.x.y.z.service.app" />

    <context:component-scan base-package="com.x.y.z.controller" />

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
    <bean
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

        <property name="viewResolvers">

            <list>

                <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">

                    <property name="viewClass"
                        value="org.springframework.web.servlet.view.JstlView" />

                    <property name="prefix" value="/WEB-INF/jsp/" />

                    <property name="suffix" value=".jsp" />
                </bean>
            </list>
        </property>
    </bean>

</beans>

共有3个答案

裴劲
2023-03-14

请确保对jsp中表单-元素enctype-属性使用了正确的引号。

咸昀
2023-03-14

您需要将其添加到SpringXML中

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
黄信厚
2023-03-14

尝试添加标题:

@RequestMapping(value=("/uploadpsd"),headers=("content-type=multipart/*"),method=RequestMethod.POST)
 类似资料:
  • 我试图使一个宁静的控制器上传文件。我看到了这一点,做了这个控制器: 然后我用邮递员发送了一份pdf: 但服务器崩溃,出现以下错误: 我再次找到了这个,并添加了一个文件 不幸的是,它仍然抱怨同样的错误。

  • 问题内容: 我试图使一个宁静的控制器来上传文件。我已经看到 了,并做了这个控制器: and then i used postman to send a pdf: But the server crashes with the error: 我再次找到了这个,并添加了一个bean.xml文件 不幸的是,它仍然抱怨相同的错误。 问题答案: 当您将邮递员用于多部分请求时,请不要 在页眉中指定自定义Con

  • 我在使用Angular 9 with Spring Boot来实现一个简单的应用程序时遇到了一个问题,该应用程序可以在相同的请求中从UI上传文件和数据。在我用基本身份验证实现安全性之前,一切都很正常。现在,在我登录并想要上载数据后,出现以下错误: <代码>组织。springframework。网状物多部分。MultipartException:当前请求不是多部分请求 标题设置为内容类型:“mult

  • 我正在尝试用angularjs上传文件,但有一个问题是“当前请求不是多部分请求”,我几乎尝试了谷歌的所有解决方案,但都没有解决我的问题,希望有人能回答我的问题,谢谢。 这是我的springMVC配置 这是角控制器 这是角服务 这是上传控制器 表单数据 -----WebKitFormBoundaryRZP8MUHA8LCBDZDN 内容-处置:表单-数据;name=“文件”;filename=“1.

  • 我有一个场景,在执行GET请求时,在从命令行调用jetty服务器时(mvn jetty:run),不返回JSON对象。有趣的是,当我使用Eclipse中安装的“Run Jetty Run”功能时,会返回JSON对象。 我在后端使用Spring rest控制器,带有以下注释: @RequestMapping(value=“/Customer”,method=RequestMethod.GET,hea

  • 我已经写了一段时间的iPhone应用程序,发送数据到服务器,接收数据(通过HTTP协议),没有想太多。理论上,我主要熟悉进程,但我不太熟悉的部分是HTTP多部分请求。我知道它的基本结构,但它的核心我不知道。 似乎每当我发送不同于纯文本的东西(如照片,音乐),我必须使用多部分请求。有人能简单地向我解释一下为什么要用它,它的优点是什么吗? 如果我使用它,为什么这样发送照片更好?