多部分Http请求

时间:2012-06-13 08:39:56

标签: java httprequest multipartform-data

我如何初始化Multipart请求..?我使用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>

2 个答案:

答案 0 :(得分:3)

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

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

您只需要转换HttpServletRequest请求。

答案 1 :(得分: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中也是如此。希望它有所帮助。