web-dev-qa-db-ja.com

リクエストにmultipart / form-dataまたはmultipart / mixedストリームが含まれていません。コンテンツタイプヘッダーはapplication / x-www-form-urlencodedです

ユーザーがアップロードしたユーザーのファイルを取得しようとしていますが、ファイルを選択して送信すると、そのエラーが発生します。

エラー:

org.Apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
        at org.Apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.Java:947)
        at org.Apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.Java:310)
        at org.Apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.Java:334)
        at org.Apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.Java:115)
        at com.example.HttpRequest.doPost(HttpRequest.Java:153)
        at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
        at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
        at Java.lang.reflect.Method.invoke(Method.Java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.Java:205)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.Java:133)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:116)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.Java:827)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:738)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:85)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:963)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:897)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:970)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.Java:872)
        at javax.servlet.http.HttpServlet.service(HttpServlet.Java:648)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:846)
        at javax.servlet.http.HttpServlet.service(HttpServlet.Java:729)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:230)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.Java:99)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.Java:105)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.Java:81)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.Java:197)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:198)
        at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:96)
        at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:474)
        at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:140)
        at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:79)
        at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:87)
        at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:349)
        at org.Apache.coyote.http11.Http11Processor.service(Http11Processor.Java:783)
        at org.Apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.Java:66)
        at org.Apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.Java:798)
        at org.Apache.Tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.Java:1434)
        at org.Apache.Tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.Java:49)
        at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1142)
        at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:617)
        at org.Apache.Tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.Java:61)
        at Java.lang.Thread.run(Thread.Java:745)
    org.Apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
        at org.Apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.Java:947)
        at org.Apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.Java:310)
        at org.Apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.Java:334)
        at org.Apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.Java:115)
        at com.example.HttpRequest.doPost(HttpRequest.Java:153)
        at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
        at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
        at Java.lang.reflect.Method.invoke(Method.Java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.Java:205)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.Java:133)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:116)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.Java:827)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:738)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:85)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:963)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:897)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:970)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.Java:872)
        at javax.servlet.http.HttpServlet.service(HttpServlet.Java:648)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:846)
        at javax.servlet.http.HttpServlet.service(HttpServlet.Java:729)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:230)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.Java:99)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.Java:105)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.Java:81)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.Java:197)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
        at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
        at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
        at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:198)
        at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:96)
        at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:474)
        at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:140)
        at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:79)
        at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:87)
        at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:349)
        at org.Apache.coyote.http11.Http11Processor.service(Http11Processor.Java:783)
        at org.Apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.Java:66)
        at org.Apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.Java:798)
        at org.Apache.Tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.Java:1434)
        at org.Apache.Tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.Java:49)
        at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1142)
        at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:617)
        at org.Apache.Tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.Java:61)
        at Java.lang.Thread.run(Thread.Java:745)

HTTP:

writer.write("<td><input type='file' id='file_input' enctype='multipart/form-data' multiple=webkitdirectory=''></td>");

dopost.Java(その主要部分)

// Create a factory for disk-based file items  
            DiskFileItemFactory factory = new DiskFileItemFactory();
            // Set factory constraints
            factory.setSizeThreshold(4096); 
            factory.setRepository(tempPathFile);
         // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

            // Set overall request size constraint
            upload.setSizeMax(4194304); 

            List<FileItem> items = upload.parseRequest(request);
            Iterator<FileItem> i = items.iterator();
            while (i.hasNext()) {
               FileItem fi = (FileItem) i.next();
               String fileName = fi.getName();
               if (fileName != null) {
                   File fullFile = new File(fi.getName());
                   File savedFile = new File(uploadPath, fullFile.getName());
                   fi.write(savedFile);
               }
            }
            System.out.print("upload succeed");
6
Cheng Gail

<form>enctype='multipart/form-data'を設定する必要があります
ではありません<input>

9
Mr.Arjun