0

我怎样才能初始化多部分请求..?我正在使用 multipart/form-data 内容类型上传文件,但我无法在控制器中获取多部分请求。那么如何在我的控制器中获取多部分请求.. 提前致谢。我收到这样的错误..

Jun 13, 2012 2:01:05 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/Login] threw  exception [Request processing failed; nested exception is java.lang.IllegalStateException:  Multipart request not initialized] with root cause
java.lang.IllegalStateException: Multipart request not initialized
at org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest.initializeMultipart(AbstractMultipartHttpServletRequest.java:107)
at org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest.getMultipartFiles(AbstractMultipartHttpServletRequest.java:97)
at org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest.getFile(AbstractMultipartHttpServletRequest.java:60)
at com.mpm.common.controller.FileUploadController.create(FileUploadController.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:279)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

我的 JSP 代码是:

    <body>
    <h1>Please upload a file</h1>
    <form method="post" action="upload.action" enctype="multipart/form-data">
        <input type="text" name="name"/></br>
        <input type="file" name="file"/></br>
        <input type="submit"/>
    </form>
</body>

我的 servlet-context.xml 代码是:

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
</bean>

<bean id="fileUploadController" class="com.mpm.common.controller.FileUploadController" ></bean>
4

2 回答 2

3

你似乎使用弹簧。在这种情况下,我通常管理这样的多部分请求:

@RequestMapping("/url")
public String method(HttpServletRequest request) {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    // do stuff with multipartRequest
    return "/jsp";
}

您只需要转换您的 HttpServletRequest 请求。

于 2012-06-13T08:47:29.447 回答
0
 public void upload(HttpServletRequest request) {

    File up = new File("C:\\temp"); // path where u need to upload

    // Create object of MultipartRequest to upload file
    MultipartRequest m;
    try {
        m = new MultipartRequest(request, up.toString());

        Enumeration files = m.getFileNames();

        // Get the files to be uploaded from enumeration
        while (files.hasMoreElements()) {
            String upload = (String) files.nextElement();
            filename = m.getFilesystemName(upload);
            // out.println("<br/><br/><br/><br/>");
        }
    } catch (IOException e) {
        System.out.println("Error in Uploading files...");
    }
      xsdName = filename.substring(0, filename.lastIndexOf('.'));
 }

这是我在 servlet 中执行相同操作的代码。希望能帮助到你。

于 2012-06-13T08:48:15.533 回答