我正在使用 Heroku 和 Spark 框架来上传 .json 文件。
HTML:
<form method="post" enctype="multipart/form-data" action="UploadJson">
<input type="file" name="import_file" accept=".json" />
<button>Upload</button>
</form>
爪哇:
post("/UploadJson", "multipart/form-data", (request, response) -> {
String location = "/public/res";
long maxFileSize = 100000000;
long maxRequestSize = 100000000;
int fileSizeThreshold = 1024;
MultipartConfigElement multipartConfigElement = new MultipartConfigElement(location, maxFileSize, maxRequestSize, fileSizeThreshold);
request.raw().setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement);
try (InputStream inputStream = request.raw().getPart("import_file").getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
)
该代码在本地工作,但在 Heroku 中部署时出现以下错误:
java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createTempFile(File.java:2024)
at org.eclipse.jetty.util.MultiPartInputStreamParser$MultiPart.createFile(MultiPartInputStreamParser.java:138)
at org.eclipse.jetty.util.MultiPartInputStreamParser$MultiPart.write(MultiPartInputStreamParser.java:116)
at org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:690)
at org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:405)
at org.eclipse.jetty.server.Request.getParts(Request.java:2311)
at org.eclipse.jetty.server.Request.getParts(Request.java:2290)
at org.eclipse.jetty.server.Request.getPart(Request.java:2279)
at javax.servlet.http.HttpServletRequestWrapper.getPart(HttpServletRequestWrapper.java:386)
at Main.lambda$main$7(Main.java:146)
异常发生在
request.raw().getPart("import_file").getInputStream() part