public static File getFileFromClassPath(String path){
File targetFile = new File(path);
if(!targetFile.exists()){
if(targetFile.getParent()!=null){
File parent=new File(targetFile.getParent());
if(!parent.exists()){
parent.mkdirs();
}
}
InputStream initialStream=null;
OutputStream outStream =null;
try {
Resource resource=new ClassPathResource(path);
//注意通过getInputStream,不能用getFile
initialStream=resource.getInputStream();
byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer);
outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
} catch (IOException e) {
LOG.error("getFileFromClassPath 失败",e);
} finally {
if (initialStream != null) {
try {
initialStream.close(); // 关闭流
} catch (IOException e) {
LOG.debug("inputStream close IOException:" + e.getMessage());
}
}
if (outStream != null) {
try {
outStream.close(); // 关闭流
} catch (IOException e) {
LOG.debug("outStream close IOException:" + e.getMessage());
}
}
}
}
return targetFile;
}