我正在使用spark开发web应用程序;当我想要上传文件时,出现了问题:
public final class SparkTesting
{
public static void main(final String... args)
{
Spark.staticFileLocation("/site");
Spark.port(8080);
Spark.post("/upload", (request, response) -> {
final Part uploadedFile = request.raw().getPart("uploadedFile");
final Path path = Paths.get("/tmp/meh");
try (final InputStream in = uploadedFile.getInputStream()) {
Files.copy(in, path);
}
response.redirect("/");
return "OK";
});
}
}但是我得到了这个错误:
[qtp509057984-36] ERROR spark.webserver.MatcherFilter -
java.lang.IllegalStateException: No multipart config for servlet
at org.eclipse.jetty.server.Request.getPart(Request.java:2039)
at javax.servlet.http.HttpServletRequestWrapper.getPart(HttpServletRequestWrapper.java:361)
at com.github.fge.grappa.debugger.web.SparkTesting.lambda$main$0(SparkTesting.java:20)
at com.github.fge.grappa.debugger.web.SparkTesting$$Lambda$1/920011586.handle(Unknown Source)
at spark.SparkBase$1.handle(SparkBase.java:264)
at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:154)
at spark.webserver.JettyHandler.doHandle(JettyHandler.java:60)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:179)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:451)
at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:252)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:266)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:240)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
at java.lang.Thread.run(Thread.java:745)即使我尝试显式地指定类型,如下所示:
Spark.post("/upload", "multipart/form-data", etc etc)它仍然会失败。
我也许可以找到一个库来解析multipart/form-data,然后抓取整个内容,然后自己解析,但这太浪费了。
我可以配置spark来处理这种情况吗?
发布于 2015-09-28 16:09:01
姚凯提供的答案是正确的,除了在使用时:
request.raw().setAttribute("org.eclipse.multipartConfig", multipartConfigElement);改用下面的代码:
request.raw().setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement);发布于 2015-05-18 02:34:09
通过添加几行代码来添加multipart配置,您可以在没有外部库的情况下处理multipart/form-data:
public Object handle(Request request, Response response) {
MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/tmp");
request.raw().setAttribute("org.eclipse.multipartConfig", multipartConfigElement);
....
Part file = request.raw().getPart("file"); //file is name of the upload form
}来源:http://deniz.dizman.org/file-uploads-using-spark-java-micro-framework/
发布于 2015-09-03 03:15:03
我使用apache commons-fileupload来处理这个问题。
post("/upload", (req, res) -> {
final File upload = new File("upload");
if (!upload.exists() && !upload.mkdirs()) {
throw new RuntimeException("Failed to create directory " + upload.getAbsolutePath());
}
// apache commons-fileupload to handle file upload
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(upload);
ServletFileUpload fileUpload = new ServletFileUpload(factory);
List<FileItem> items = fileUpload.parseRequest(req.raw());
// image is the field name that we want to save
FileItem item = items.stream()
.filter(e -> "image".equals(e.getFieldName()))
.findFirst().get();
String fileName = item.getName();
item.write(new File(dir, fileName));
halt(200);
return null;
});请参阅https://github.com/perwendel/spark/issues/26#issuecomment-95077039
https://stackoverflow.com/questions/29373468
复制相似问题