Current Position:Home > Barcode Image display in email/jsp

Barcode Image display in email/jsp

Update:11-30Source: network consolidation
Advertisement
Hi,
I want to display the barcode image in the email/jsp.
I have created one droplet. This will generate the barcode image and will return as a OutputStream.
I am trying to convert this OutputStream as a byte array and setting in a request param. When i access this param in the jsp and trying to display as a image using dsp:valueof or dsp:img tag, i am getting only the byte array whatever i have set in the droplet instead of image.
I have set the content type as image/jpeg also in the request object.
I have tried to write the converted byte array in a response object also and accessed the droplet in jsp.
But i am not getting the image in the jsp. I am getting only the byte array in the jsp.
Please suggest me any other solution to resolve this.
Thanks,
Saravanan

The Best Answer

Advertisement
If you have a binary array for image, you can try to use a servlet for rendering the image directly from the byte array. Doing it through a separate servlet would ensure that we do not run into the issue of mixing character and binary based response streams. Here is the pseudo code for doing this.
In Servlet:
BarcodeImageHandlerServlet extends extends HttpServletService {
public void service(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
  byte [] imageByteArr = getImage(); //your image byte array
  String name= "barcode-01";
  response.setContentType("image/jpeg");
  response.setContentLength(imageByteArr.length);
  response.setHeader("Content-Disposition", "inline; filename=\"" + name + "\"");
  BufferedInputStream input = null;
  BufferedOutputStream output = null;
  try {
    input = new BufferedInputStream(new ByteArrayInputStream(imageByteArr));
    output = new BufferedOutputStream(response.getOutputStream());
    output.write(imageByteArr);
  catch (IOException e) {
  finally {
    if (output != null) {
      try {
        output.close();
      catch (IOException iex) {
    if (input != null) {
      try {
        input.close();
      catch (IOException iex) {
}Create following component configurations in your application's config layer to add your servlet using ServletPathServlet and ServletPathDispatcher:
/atg/myapp/BarcodeImageServlet.properties:
$class=com.myapp.BarcodeImageHandlerServlet
$scope=global
/atg/dynamo/servlet/pipeline/ServletPathDispatcher.properties:
dispatcherServiceMap+=\
/barcode-image-servlet\=/atg/myapp/BarcodeImageServlet
/atg/dynamo/servlet/pipeline/ServletPathServlet.properties:
servletPaths+=/barcode-image-servlet
Then in your JSP you can call it as:
<img src="<c:out value="${pageContext.request.contextPath}"/>/dyn/barcode-image-servlet" />