2017-08-14 818 views
0

我嘗試通過這種方式創建CommonsMultipartFile實例,但發生了java.lang.NullPointerException。如何創建CommonsMultipartFile實例

DiskFileItemFactory factory = new DiskFileItemFactory(1024*1024,null); 
FileItem fileitem = factory.createItem("file", "application/vnd.ms-excel;charset=utf-8", true, "d:\\a.xls");//Method threw 'java.lang.NullPointerException' exception. Cannot evaluate org.apache.commons.fileupload.disk.DiskFileItem.toString() 
    CommonsMultipartFile commonFile=new CommonsMultipartFile(fileitem); 
+0

你不應該自己創建它。你爲什麼需要這個? –

+0

是的,我不應該自己創建它。而'java.lang.NullPointerException'的共振是沒有OutputStream獲取。「fileitem.getOutputStream();」解決問題。 – newCheng

回答

0

您不能將null作爲第二個參數傳遞給構造函數。

傳遞某種臨時文件夾中,像這樣:

DiskFileItemFactory factory = new DiskFileItemFactory(1024*1024, Files.createTempDirectory("mytemp").toFile());