之前好几篇帖子都是关于根据模板生成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;
}