java读取resource下的模板文件

需要获取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;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值