How to show byte[] as a image in angularjs

Convert byte [] to String :


String encodedImage = Base64.encode(res);

then we have to show the encodedImage in angular js file :
<img class=’imagem_artigo’
alt=”IMG DESC”>


How to convert image to byte array and byte array to image in Java

The conventional way of including an image on a webpage is to store an image on server or database and give its path on html webpage so that the image is obtained from that place and then shown on the webpage.

This article will help you to show images on your webpage’s without having those images stored in your server or database.

This is all possible because image can be converted into sequence of byte array. And the converted byte array can be used to get back the original image.

This byte array converter can be made easily in Java which we will discuss here. After that we will show how to use that byte array to display image on web page.

First we make a Java program which is going to input an image and then convert it into byte array and then encode it using Base 64 encoding which is standard.

Listing 1: Java code to get byte Array

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Image2Base64 {
public static void main(String args[]) {
      BufferedImage image = File("icon.png"));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(image, "png", baos);
     byte[] res=baos.toByteArray();
      String encodedImage = Base64.encode(baos.toByteArray());
      System.out.println(“The encoded image byte array is shown below.Please use this in your webpage image tag.\n”+encodedImage);
catch(Exception e) {
System.out.println(“Some problem has occurred. Please try again”);

We give a short description of what is happening in the code ,

  1. First we explain the import files.
  2. BufferedImage is used to store the image(in our case icon.png) in a buffer.
  3. This is used to convert the image in byte array
  4. File is used to read a file.
  5. ImageIO is used to write the buffered image into byte output array
  6. Base 64 is used to encode the byte array obtained in Base 64 encoding.
  7. The name of the class is Image2Base64 .We define a main function.
  8. The image to be read is icon.png
  9. First we read the image and store it inside a Buffered image variable which is image.

  10. Now we write the buffered image into a ByteArrayOutputStream object with help of ImageIO class so that it can be converted into a byte array
  11. Now we convert it into byte array.
  12. Now we encode this using Base64 encoding
  13. Then we display the byte array for image on screen.Remember the converted byte array strin is a very long string.
  14. You need to copy this output and then use it in the html webpage to include images(I will show that).Make sure you copy it properly because if anything is missed then image will not be displayed or will be distorted and hence will be of no use.


Now we come to HTML part and tell how to show using a byte array.

Listing 2: Web page using byte Array

<img class='imagem_artigo'  src="data:image/png;base64,PASTE THE BYTE ARRAY HERE WHICH IS OBTAINED FROM JAVA PROGRAM" alt="IMG DESC">

Here, I have shown the html which will be used.

You can define any title you like. Here I have given it as csanuragjain but you may change that in your webpage so that it may reflect appropriate message to the user.

You just need to copy the output of the java program in the area in Bold. The output would be a very long string like iVBORw0KGgoAAAANSUhEUgAAAEAAAAA………………

Now , you may store that byte array in your database but remember that this is a very long string.If you want to use it later in your webpage like in php or jsp then just connect with mysql and get the byte array value from there and then put the value obtained in your own html image tag.

Sample Output :

Sample output

Explaination :

We founded the byte array from the above java programs.It does all the work and at last generates an encoded byte array which could be used easily.

Usage :

You may make a backup of your image so that you always have the byte array to resemble to your favorite images

This is all for this article. Hope you liked it. See you next time with some new exciting article.


pessimistic locking in java

Add the bellow properties into your model class  :

@Column(name = “version_lock”, nullable = false)
long versionLock;


Add the bellow line before your model class  :

name = “unit_master”,
uniqueConstraints = {
@UniqueConstraint(columnNames = {“unit_code”}, name=”UK_UNIT_UNITCODE”),
@UniqueConstraint(columnNames = {“unit_name”}, name=”UK_UNIT_UNITNAME”)
@JsonIgnoreProperties({“hibernateLazyInitializer”, “handler”})
public class UnitMaster implements Serializable{ 1970-01-01 05:30:00

Date cannot be send as a json.


1)Date make as a String.

example :

Date openDate;

changed to

String openDate;


2)use @JsonIgnore

example :

/*Create/Last User and Data Info*/
SystemTrack systemTrack;


package com.efreightsuite.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import com.efreightsuite.util.JsonDateSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import lombok.Data;


public class SystemTrack implements Serializable {
private static final long serialVersionUID = 1L;

/*Created User Name */
@Column(name=”create_user”, length=30, nullable=false)
String createUser;

/*Created Date */
@Column(name=”create_date”, nullable=false)
@JsonSerialize(using = JsonDateSerializer.class)
Date createDate;

/*Last Updated User Name */
@Column(name=”last_updated_user”, length=30, nullable=false)
String lastUpdatedUser;

/*Last Updated Date */
@Column(name=”last_updated_date”, nullable=false)
@JsonSerialize(using = JsonDateSerializer.class)
Date lastUpdatedDate;










Redis server port already in use

issue :

achyu@prabhu:~$ redis-server
20362:C 26 Apr 16:10:13.639 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
20362:M 26 Apr 16:10:13.640 * Increased maximum number of open files to 10032 (it was originally set to 1024).
20362:M 26 Apr 16:10:13.640 # Creating Server TCP listening socket *:6379: bind: Address already in use


solution :

i)redis-cli ping
ii)redis-cli shutdown


Stored Function to generate Sequences

Today a customer asked me to help them to convert their sequence generation process to the stored procedure and even though I have already seen it somewhere I did not find it with two minutes of googling so I wrote a simple one myself and posting it here for public benefit or my later use :)

This implementation uses single table to maintain multiple sequences which application can use in desired way, though you of course could have them using different tables.

I use MyISAM tables here which allows to use such sequences in transactions without serializing transactions which require access to same sequence, though it is not as safe as if you use Innodb table in this case.

Even though implementation is just 2 lines of code it seems to confuse a lot of people becauselast_insert_id() is used rather unusual way – with argument. This way of using this function allows you to “inject” the value to be returned next time this function is called.

Sometimes people wonder why you would like to use sequences instead of MySQL auto_increment columns ? Leaving aside more exotic ways of sequences even pure sequential value as in the case above can be quite helpful – in MySQL 5.0 you may with to use them instead of auto_increment with Innodb tables to avoid short term “table level locks” which innodb sets when Insert is happening in the table with auto_increment values. It is also helpful if you need to decouple ID generation from storing the data – for example IDs are generated on central server and when stored on number of servers or to number of individual tables.
Finally your own sequences allow you to generate multiple sequence values on demand with single statement for one or more sequences, which also can be helpful:

I should note this sequence generation requires serialization, though it is short term but it may still become the bottleneck for application with high sequence use rate. For such heavy duty apps I would use another approaches – in particular allocating “ranges” of sequences and caching them in applications, using UUID_SHORT() and other methods which do not require global lock for each time new sequence value needs to be retrieved.