问题描述
我正在尝试将文件上传到我的Spark Maven项目src / main / resources中,但始终将其上传到外部。我需要能够使用localhost:4567 / abc.png这样的URL访问上传的文件。我在src / main / resources下创建了公共目录。使用staticFiles.location(“ public”)或staticFiles.externalLocation(“ public”),如果我在此处创建公共目录,则文件永远不会上传到此路径,而是上传到项目路径,例如E:\ eclipseSpaceNew \ microweb。以下是上传方法:
private static String upload(Request request,Response response) {
String location = "public"; // 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
Collection<Part> parts = null;
MultipartConfigElement multipartConfigElement = null;
Part uploadedFile = null;
try {
multipartConfigElement = new MultipartConfigElement(location,maxFileSize,maxRequestSize,fileSizeThreshold);
request.raw().setAttribute("org.eclipse.jetty.multipartConfig",multipartConfigElement);
parts = request.raw().getParts();
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("file").getSubmittedFileName();
System.out.println("Title: " + request.raw().getParameter("title"));
System.out.println("File: " + fName);
uploadedFile = request.raw().getPart("file");
Path out = Paths.get("public/" + fName);
try (final InputStream in = uploadedFile.getInputStream()) {
Files.copy(in,out);
System.out.println("filename =" + out.getFileName());
System.out.println("path = public/" + fName );
System.out.println("link=" + request.host() + "/" + fName);
//uploadedFile.delete();
}
} catch (IOException | servletexception e) {
e.printstacktrace();
return "Error uploading file" + e.getMessage();
} finally {
// cleanup
multipartConfigElement = null;
parts = null;
uploadedFile = null;
}
return "OK";
}
有人可以帮我解决这个问题吗?谢谢
-狂奔
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)