(五)文件模板用流存放到数据库中

之前好几篇帖子都是关于根据模板生成pdf。模板是放在项目中,和项目一起打包成jar的。

当模板比较少的时候,可以将模板放到数据库中。把模板转成字节数组存储。

	/**
     * 保存本地文件模板到数据库中
     * @param templatePath
     * @return
     */
    public static DocumentTemplete uploadAttachmentTempleteToDatabase(String templatePath){
        File  file = new File(templatePath);
        try {
            byte data[] =  this.fileToByte(file);
            return new DocumentTemplete(data);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

	/**
     * 将文件内容转成字节数组
     */
    public static byte[] fileToByte(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        ByteArrayOutputStream bAOutputStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bAOutputStream.write(ch);
        }
        byte data[] = bAOutputStream.toByteArray();
        bAOutputStream.close();
        return data;
    }

	/**
	 * 文件模板
	 */
	@Data
	public class DocumentTemplete{
	    /**
	     * 文件名
	     */
	    private String templeteName;
	
	    /**
	     * 文件内容
	     */
	    private byte[] content;
	
	    /**
	     * 文件类型
	     */
	    private TEMPLETE_TYPE type;
	
	    /**
	     * 文件描述
	     */
	    private String description;
	
	    /**
	     * 文件编码形式
	     */
	    private String encoding;
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值