2 回答

TA贡献1829条经验 获得超7个赞
File在 Java 中使用 a 时,它必须指向操作系统文件系统上的物理文件。运行未打包的应用程序时就是这种情况,但是在运行时jar它不是物理文件,因此将无法工作(导致错误或空指针异常)
使用ResourceSpring的类之一来访问资源。在这种情况下,因为它来自您想要使用的类路径ClassPathResource。然后直接使用InputStream读取文件。
Resource input = new ClassPathResource(“static/doc/modal/model.xls”);
InputStream in = input.getInputStream();
// Use InputStream to read file
这将作为打包和未打包的应用程序工作。不要使用,getFile因为这在打包时不起作用。

TA贡献1817条经验 获得超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;
在评论中讨论希望这可以帮助你。
添加回答
举报