栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java读取resource下的模板文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
    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;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1013730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号