2 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個贊
File在 Java 中使用 a 時,它必須指向操作系統(tǒng)文件系統(tǒng)上的物理文件。運(yùn)行未打包的應(yīng)用程序時就是這種情況,但是在運(yùn)行時jar它不是物理文件,因此將無法工作(導(dǎo)致錯誤或空指針異常)
使用ResourceSpring的類之一來訪問資源。在這種情況下,因?yàn)樗鼇碜阅胍褂玫念惵窂紺lassPathResource。然后直接使用InputStream讀取文件。
Resource input = new ClassPathResource(“static/doc/modal/model.xls”);
InputStream in = input.getInputStream();
// Use InputStream to read file
這將作為打包和未打包的應(yīng)用程序工作。不要使用,getFile因?yàn)檫@在打包時不起作用。

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個贊
File xlsfile = new ClassPathResource("/doc/modal/model.xls").getFile();
//also check the path
//String currentPath = xlsfile .getAbsolutePath();
File newXls=new File("classpath:static/doc/temp");
newXls=xlsfile;
在評論中討論希望這可以幫助你。
添加回答
舉報