Image download servlet
Requirement : In Application we need to show image in image component. I have followed below approach. Created servlet and mapped its url to image source attribute. Please follow below screen shot.
and ImageServlet code..
public class ImageServlet extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg; charset=UTF-8";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
String appModuleName = "yash.hr.model.am.AppModule";
String appModuleConfig = "AppModuleLocal";
ApplicationModule am = null;
ViewObject vo = null;
try{
String emp_id=request.getParameter("emp_id");
am = Configuration.createRootApplicationModule(appModuleName, appModuleConfig);
vo = am.findViewObject("AccessImageVO1");
if (vo == null)
{
throw new Exception("ImageAccessView1 not found!");
}
vo.ensureVariableManager().setVariableValue("emp_id",emp_id );
vo.executeQuery();
if(vo!=null&&vo.getRowCount()>0){
BlobDomain image = null;
image=(BlobDomain) vo.first().getAttribute("ImageData");
response.setContentType(CONTENT_TYPE);
OutputStream outputStream = response.getOutputStream();
IOUtils.copy(image.getInputStream(), outputStream);
// cloase the blob to release the recources
image.closeInputStream();
// flush the outout stream
outputStream.flush();
}
}catch(Exception e){
e.printStackTrace();
} finally
{
if (am != null)
{
//Release the appModule
Configuration.releaseRootApplicationModule(am, true);
}
}
}
}
and ImageServlet code..
public class ImageServlet extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg; charset=UTF-8";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
String appModuleName = "yash.hr.model.am.AppModule";
String appModuleConfig = "AppModuleLocal";
ApplicationModule am = null;
ViewObject vo = null;
try{
String emp_id=request.getParameter("emp_id");
am = Configuration.createRootApplicationModule(appModuleName, appModuleConfig);
vo = am.findViewObject("AccessImageVO1");
if (vo == null)
{
throw new Exception("ImageAccessView1 not found!");
}
vo.ensureVariableManager().setVariableValue("emp_id",emp_id );
vo.executeQuery();
if(vo!=null&&vo.getRowCount()>0){
BlobDomain image = null;
image=(BlobDomain) vo.first().getAttribute("ImageData");
response.setContentType(CONTENT_TYPE);
OutputStream outputStream = response.getOutputStream();
IOUtils.copy(image.getInputStream(), outputStream);
// cloase the blob to release the recources
image.closeInputStream();
// flush the outout stream
outputStream.flush();
}
}catch(Exception e){
e.printStackTrace();
} finally
{
if (am != null)
{
//Release the appModule
Configuration.releaseRootApplicationModule(am, true);
}
}
}
}
Cheers...
Kiran.
Comments
Post a Comment