Monday, 29 July 2013

Fetch Image From Data Base In JAVA Spring

Fetch Image from data base in Java or Spring 

<img alt="User Image" id="UserImage" src="/person/getPhoto?id=${client.id}" width="150" height="150" >

In above image tag the marked data will call the controller method with ID of Person whose image you want to get



@RequestMapping(method = RequestMethod.GET, value = "getPhoto")
public void getPhoto(HttpServletRequest request,
HttpServletResponse response) throws FileUploadException,
IOException {
int id = 0;
Person pi = null;
 id = Integer.parseInt(request.getParameter(ID));
pi = PersonService.getCaregiver(id).getPerson();   //get photo form db
response.setContentType("image/jpg");
response.getOutputStream().write(pi.getPhoto());
response.getOutputStream().flush();
response.getOutputStream().close();
}