(三)spingboot打成jar之后。读取不到相对路径

spingboot打成jar之后,读取不到相对路径。eg.Resource下文件

在本地项目启动的时候,可以使用AttachmentUtil.class.getClassLoader().getResource(path);读取到文件。

但是项目打成jar之后,读取不到文件。会报错java.io.FileNotFoundException

主要原因是springboot内置tomcat,打包后是一个jar包,因此通过文件读取获取流的方式行不通,因为无法直接读取压缩包中的文件,读取只能通过流的方式读取
ClassPathResource classPathResource = new ClassPathResource(path);
InputStream inputStream =classPathResource.getInputStream();

1.当你要将文件内容读取成字符串时

/**
     * 读取指定路径的模板文件为String
     */
    public String getTemplateHtmlString(String path) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource(path);
        InputStream inputStream =classPathResource.getInputStream();
        if (inputStream == null) {
            throw new GeneralBusinessException("找不到html模板!");
        }
        try {
            StringBuilder builder = new StringBuilder();
            InputStreamReader reader = new InputStreamReader(inputStream , "UTF-8" );
            BufferedReader bfReader = new BufferedReader( reader );
            String tmpContent = null;
            while ( ( tmpContent = bfReader.readLine() ) != null ) {
                builder.append( tmpContent );
            }
            bfReader.close();
            return builder.toString() ;
        } catch ( UnsupportedEncodingException e ) {
            log.error("Files.toString出错!", e);
            return null;
        }

    }




2.当直接读取流到文件中

   /**
     * 利用表单域生成pdf
      * @param templatePath   模板路径
     * @param replacement       pdf中存放的数据
     */
    public static byte[] createPdf(String templatePath, Object replacement){
        try {
            ClassPathResource classPathResource = new ClassPathResource(templatePath);
            InputStream inputStream =classPathResource.getInputStream();

            /* 打开已经定义好字段以后的pdf模板 */
            PdfReader reader = new PdfReader(inputStream);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfStamper stamp = new PdfStamper(reader,baos);

            /* 使用中文字体 */
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
            Font FontChinese = new Font(bf, 12, Font.NORMAL);

            /* 取出报表模板中的所有字段 */
            AcroFields form = stamp.getAcroFields();
            form.addSubstitutionFont(bf);

            for (Field aField : replacement.getClass().getDeclaredFields()) {
                String fieldName = aField.getName();
                if ("class java.lang.String".equals(aField.getGenericType().toString())) {
                    Method method = replacement.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
                    String fieldValue = (String) method.invoke(replacement);
                    if (fieldValue != null) {
                        form.setField(fieldName,fieldValue);
                    }
                }
            }
            /* 如果为false那么生成的PDF文件还能编辑,一定要设为true*/
            stamp.setFormFlattening(true);
            stamp.close();
            reader.close();
            baos.close();
            return baos.toByteArray();
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

3.SpringBoot读取Resource下文件的几种方式

方法一:(推荐:本地和打成jar都可以读取到)

 	  ClassPathResource classPathResource = new ClassPathResource(templatePath);
      InputStream inputStream =classPathResource.getInputStream();

方法二:(推荐:本地不可以,打成jar都可以读取到)

	  InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excleTemplate/test.xlsx");
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值