我想用 spring 将 File 转换为 multipartfile。我做了这个:
File in;
MultipartFile file = null;
in = new File("C:...file on disk");
int size = (int) in.length();
DiskFileItem fileItem = new DiskFileItem("file", "application/vnd.ms-excel", false, nomefile, size ,in.getAbsoluteFile());
file = new CommonsMultipartFile(fileItem);
但收到此异常:
threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.apache.commons.fileupload.disk.DiskFileItem.getSize(DiskFileItem.java:316)
我认为 fileItem 为 null 但在调试模式下已填充,还有另一种解决方案吗?我有这篇文章Converting File to MultiPartFile但不工作且没有解决方案。