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

馋奶兔 提交于 2019-12-23 18:08:56

问题


I want to upload a file from an html form using spark. Following is my java function to handle the post route:

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 :

<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:

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

I'm using eclipse IDE and tomcat server.

Please help me with this problem.


回答1:


I just found out that as I'm using tomcat server with spark, I had set up filter i.e. 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.

Please see the following links for reference:

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

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

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

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




回答2:


You also have to provide multipart config settings in your Servlet configuration inside the web.xml file. 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>


来源:https://stackoverflow.com/questions/40932829/spark-java-unable-to-process-parts-as-no-multi-part-configuration-has-been-prov

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!