简体   繁体   English

Spark Java:由于未提供多部件配置,因此无法处理部件

[英]Spark Java: Unable to process parts as no multi-part configuration has been provided

I want to upload a file from an html form using spark. 我想使用spark从html表单上传文件。 Following is my java function to handle the post route: 以下是我的java函数来处理post路由:

Spark.post("/upload", "multipart/form-data", (request, response) -> {

        String location = "temporary";          // the directory location where files will be stored
        long maxFileSize = 100000000;       // the maximum size allowed for uploaded files
        long maxRequestSize = 100000000;    // the maximum size allowed for multipart/form-data requests
        int fileSizeThreshold = 1024;       // the size threshold after which files will be written to disk

        MultipartConfigElement multipartConfigElement = new MultipartConfigElement(
             location, maxFileSize, maxRequestSize, fileSizeThreshold);
         request.raw().setAttribute("org.eclipse.multipartConfig",
             multipartConfigElement);

        Collection<Part> parts = request.raw().getParts();  //Line 50 where error is there
        for (Part part : parts) {
           System.out.println("Name: " + part.getName());
           System.out.println("Size: " + part.getSize());
           System.out.println("Filename: " + part.getSubmittedFileName());
        }

        String fName = request.raw().getPart("xmlfile").getSubmittedFileName();
        System.out.println("Title: " + request.raw().getParameter("title"));
        System.out.println("File: " + fName);

        Part uploadedFile = request.raw().getPart("xmlFile");
        Path out = Paths.get("temporary/" + fName);
        try (final InputStream in = uploadedFile.getInputStream()) {
           Files.copy(in, out);
           uploadedFile.delete();
        }
        // cleanup
        multipartConfigElement = null;
        //parts = null;
        uploadedFile = null;

        return "OK";
});

Following is the HTML form : 以下是HTML表单:

<form class="ui fluid action input" id="fileForm" method="post" action="/sparkapp/upload" enctype = "multipart/form-data">
    <input type="text" name="filePath" readonly>
    <input type="file" name="xmlFile">
    <button type="submit" value="Submit">
 </form>

When I'm uploading a file, I'm getting 500: Internal server error with the following stack trace: 当我上传文件时,我得到500:内部服务器错误,包含以下堆栈跟踪:

java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
    at org.apache.catalina.connector.Request.parseParts(Request.java:2734)
    at org.apache.catalina.connector.Request.getParts(Request.java:2701)
    at org.apache.catalina.connector.Request.getPart(Request.java:2885)
    at org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1089)
    at javax.servlet.http.HttpServletRequestWrapper.getPart(HttpServletRequestWrapper.java:362)
    at com.amulya.Application$2.handle(Application.java:50)
    at spark.RouteImpl$1.handle(RouteImpl.java:61)
    at spark.http.matching.Routes.execute(Routes.java:61)
    at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:127)
    at spark.servlet.SparkFilter.doFilter(SparkFilter.java:173)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    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)

Followed the following question but the answer didn't work: SparkJava: Upload file did't work in Spark java framework 遵循以下问题,但答案不起作用: SparkJava:上传文件在Spark java框架中不起作用

I'm using eclipse IDE and tomcat server. 我正在使用eclipse IDE和tomcat服务器。

Please help me with this problem. 请帮我解决这个问题。

I just found out that as I'm using tomcat server with spark, I had set up filter ie spark.servlet.SparkFilter . 我刚刚发现,当我使用带有spark的tomcat服务器时,我设置了过滤器,即spark.servlet.SparkFilter

Through this answer I found out that actually, I needed to set 通过这个答案我发现实际上,我需要设置

allowCasualMultipartParsing="true"

in the webapp's <Context> element in Webapp/META-INF/context.xml or Tomcat/conf/server.xml so that Tomcat should automatically parse multipart/form-data request bodies when HttpServletRequest.getPart* or HttpServletRequest.getParameter* is called, even when the target servlet isn't marked with the @MultipartConfig annotation. Webapp/META-INF/context.xmlTomcat/conf/server.xml中的webapp的<Context>元素中,以便在调用HttpServletRequest.getPart*HttpServletRequest.getParameter*时,Tomcat应自动解析multipart/form-data请求体即使目标servlet没有用@MultipartConfig注释标记。

Please see the following links for reference: 请参阅以下链接以供参考:

http://sparkjava.com/documentation.html#other-webserver http://sparkjava.com/documentation.html#other-webserver

https://stackoverflow.com/a/8050589/2256258 https://stackoverflow.com/a/8050589/2256258

http://tomcat.apache.org/tomcat-7.0-doc/config/context.html http://tomcat.apache.org/tomcat-7.0-doc/config/context.html

https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-context-xml-configuration-example/ https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-context-xml-configuration-example/

You also have to provide multipart config settings in your Servlet configuration inside the web.xml file. 您还必须在web.xml文件中的Servlet配置中提供多部分配置设置。 I have listed an example below: 我在下面列举了一个例子:

<servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.example.servlet.MyServlet</servlet-class>
        <multipart-config>
                <max-file-size>xxxxx</max-file-size>
                <max-request-size>yyyyy</max-request-size>
        </multipart-config>
</servlet>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于未提供多部件配置,因此无法处理部件 - Unable to process parts as no multi-part configuration has been provided Spring Boot 2.x 报告 java.lang.IllegalStateException:无法处理部件,因为未提供多部件配置 - Spring Boot 2.x reports java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided 即使已存在multipartResolver,也无法提供多部分配置,因此无法处理部分 - Unable to process parts as no multi-part configuration has been provided even while existing multipartResolver Spring MVC文件上传:由于未提供多部分配置,因此无法处理部分 - Spring MVC file upload: Unable to process parts as no multi-part configuration has been provided 无法解析多部分 servlet 请求;.IllegalStateException:无法处理部分,因为未提供多部分配置 - Could not parse multipart servlet request;.IllegalStateException: Unable to process parts as no multi-part configuration has been provided 无法处理部件,因为没有提供多部件配置 enctype='multipart/form-data' 使用问题 - Unable to process parts as no multi-part configuration has been provided enctype='multipart/form-data' usage issue 部分成功完成后,S3 分段上传在完成时失败 - S3 Multi-part Upload fails on completion after parts have successfully been completed 带有Spring Boot的Jersey多部分配置 - Jersey multi-part configuration with Spring Boot Spring Security未检测到多部分配置 - Spring Security doesn't detect multi-part configuration 多部分请求 - Multi-Part Request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM