9
  • Spring 3.1.1.RELEASE
  • 码头 7.3.0.v20110203
  • 公共文件上传 1.2.1
  • 公共-io 1.4

我想通过 html 表单上传两个文件,但我总是得到一个多部分请求异常。我google了很多,但没有找到正确的解决方案。

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”&gt;
                        <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>
4

4 回答 4

9

尝试添加标题:

@RequestMapping(value=("/uploadpsd"),headers=("content-type=multipart/*"),method=RequestMethod.POST)
于 2015-02-26T10:24:04.297 回答
8

您需要将此添加到您的 spring xml

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
于 2012-08-16T09:06:28.963 回答
3

请确保您在 jspenctype中为 -element 的 -attribute使用正确的引号。form

于 2013-08-05T15:19:46.380 回答
1
于 2016-06-08T11:15:08.063 回答