服务器读取resource下的文件路径报错
本地Windows读取是没问题的,但是在服务器springboot是jar包的形式,所以会有问题
解决方式
网上找了很多文章都没解决,最后亲测此方法没问题。
本地Windows读取是没问题的,但是在服务器springboot是jar包的形式,所以会有问题
ClassPathResource classPathResource = new ClassPathResource("word/"+wordName);
InputStream inputStream = classPathResource.getInputStream();
File file = asFile(inputStream);//手动转换:InputStream To File
String path = file.getPath();
/**
* InputStream 转为 file
* @param in
* @return
* @throws IOException
*/
public static File asFile(InputStream in) throws IOException {
File tempFile = File.createTempFile("whitelist", ".tmp");
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
IOUtils.copy(in, out);
return tempFile;
}