Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use Input stream or BLOB binary stream as velocity template file with Example
12007 Views
Hints 
In this example how to use blob data instead of vm file from FilePath.

RuntimeServices Class provides the interface to the Template class and template parse the blob information via StringBuffer castring. DB based blob data can use as template instead of using .vm files.
Example
Blob blob = new SerialBlob("Username is $username".getBytes());

RuntimeServices rs = RuntimeSingleton.getRuntimeServices();            
StringReader sr = new StringReader(IOUtils.toString(blob.getBinaryStream()));
SimpleNode sn = rs.parse(sr, "User Information");

Template t = new Template();
    t.setRuntimeServices(rs);
    t.setData(sn);
    t.initDocument();

VelocityContext vc = new VelocityContext();
vc.put("username", "John");

StringWriter sw = new StringWriter();
t.merge(vc, sw);
    
System.out.println(sw.toString());
BLOB Binary stream data 
Username is $username
Output 
Username is John
Best Lessons of "Java Apache Velocity Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details