需要获取resources/template路径下的文件,读取后再写入,返回给前端
本地代码获取方式:
String templateFilePath = Thread.currentThread().getContextClassLoader().getResource(uploadPath).getPath().concat("EquipmentFile.docx");
这种方式,在打包到生产环境后,由于resource路径的改变,是没办法获取到模板文件的,所以生产环境就需要先获取到文件,再获取到文件的路径
// 获取模板配置文件路径
String uploadPath = "template/".concat("EquipmentFile.docx");
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(uploadPath);
File file = InputStreamToFile(is);
String templateFilePath = file.getAbsolutePath();
logger.info("获取设备信息模板配置路径文件 id="+id+"文件路径"+templateFilePath);
/**
* 将文件流写入文件中
* @param is
*/
private File InputStreamToFile(InputStream is) throws Exception{
File file = File.createTempFile("EquipmentFile", ".docx");
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
while ((is.read(b)) != -1) {
fos.write(b);// 写入数据
}
is.close();
fos.close();// 保存数据
return file;
}