Spark Java:由于未提供多零件配置,因此无法处理零件 [英] Spark Java: Unable to process parts as no multi-part configuration has been provided

查看:156
本文介绍了Spark Java:由于未提供多零件配置,因此无法处理零件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用spark从html表单上载文件.以下是我的Java函数来处理发布路线:

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";
});

以下是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>

上传文件时,我得到500:内部服务器错误,并显示以下堆栈跟踪:

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)

遵循以下问题,但答案不起作用: SparkJava:上载文件在Spark Java框架中不起作用

Followed the following question but the answer didn't work: SparkJava: Upload file did't work in Spark java framework

我正在使用Eclipse IDE和tomcat服务器.

I'm using eclipse IDE and tomcat server.

请帮助我解决这个问题.

Please help me with this problem.

推荐答案

我刚刚发现,当我使用带有spark的tomcat服务器时,我已经设置了过滤器,即spark.servlet.SparkFilter.

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"

,以便Tomcat在调用HttpServletRequest.getPart*HttpServletRequest.getParameter*时自动解析multipart/form-data请求主体,即使目标servlet未被标记为@MultipartConfig注释.

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/

这篇关于Spark Java:由于未提供多零件配置,因此无法处理零件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆