gpt4 book ai didi

java - 星火Java : Unable to process parts as no multi-part configuration has been provided

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:51:51 29 4
gpt4 key购买 nike

我想使用 spark 从 html 表单上传文件。以下是我处理发布路由的 java 函数:

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:内部服务器错误,堆栈跟踪如下:

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: Upload file did't work in Spark java framework

我正在使用 eclipse IDE 和 tomcat 服务器。

请帮我解决这个问题。

最佳答案

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

通过this answer我发现实际上,我需要设置

allowCasualMultipartParsing="true"

在 webapp 的 <Context> Webapp/META-INF/context.xml 中的元素或 Tomcat/conf/server.xml这样Tomcat应该自动解析multipart/form-data HttpServletRequest.getPart* 时请求正文或 HttpServletRequest.getParameter*被调用,即使目标 servlet 没有用 @MultipartConfig 标记注释。

请参阅以下链接以供引用:

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/

关于java - 星火Java : Unable to process parts as no multi-part configuration has been provided,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40932829/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com