我正在使用 java-spark 来创建 Rest Api,但我无法弄清楚如何接收文件以便处理它。还没有找到像 Spring 中那样处理 MultipartFile
的东西。而且这个项目是在 Tomcat 服务器上运行的。
最佳答案
根据 official documentation ,以下代码可帮助您入门:
post("/yourUploadPath", (request, response) -> {
request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
try (InputStream is = request.raw().getPart("uploaded_file").getInputStream()) {
// Use the input stream to create a file
}
return "File uploaded";
});
关于Java Spark REST api上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51144446/