3

I have a bytearray image.

I need to show that image in jpg format in jsp page and while clicking the image, i can download the image to my pc:

I am loading the image from my mysql db as byte array..

My code is

     ResultSet res = statement.executeQuery("SELECT * FROM 
   upload_data where user_id = "+userID);
   while (res.next()) {

 contactDetails = new ContactDetails();

contactDetails.setContactPhoto(res.getBytes("photo"));

byteArrayBackToImage1(res.getBytes("photo"));
 contactsList.add(contactDetails);
}

public void byteArrayBackToImage1(byte[] imageInByte){
try{

     Random rand = new Random();
        int numNoRange = rand.nextInt();
        String number = String.valueOf(numNoRange);
    //convert byte array back to BufferedImage


    InputStream in = new ByteArrayInputStream(imageInByte);
    BufferedImage bImageFromConvert = ImageIO.read(in);
    System.out.println("bImageFromConvert : "+bImageFromConvert);

    /*ImageIO.write(bImageFromConvert, "jpg", 
             new File("c:\\"+number+".jpg")); */


}catch (Exception e) {
    // TODO: handle exception
}

I need to show the image in jsp as

eg: image.jpg image2.jpg

and by clicking image.jsp , i can download that image and save it to my pc

Please help

jennifer
  • 8,133
  • 22
  • 69
  • 96
  • 1
    in response to *I have a list of images from the db corresponding to a particular id* , You need to uniquely identify image. – jmj Mar 09 '11 at 10:05
  • Related: http://stackoverflow.com/questions/2340406/retrieve-multiple-images-from-mysql/2341322#2341322 – BalusC Mar 09 '11 at 12:31
  • @ jigar @BalusC it would be a great help if you can provide some inputs on http://stackoverflow.com/questions/11124540/some-questions-related-to-implementation-of-image-inside-email-signature. Thanks in advance. – M Sach Jun 20 '12 at 18:01

2 Answers2

14

The HTML you generate in your JSP must contain an img element with an src pointing to the URL of a servlet or action which will load the image from the database and send it to the ouput stream with the image/jpeg content type.

// in your HTML :
<img src="/getImage.action?imageId=${id_of_the_image}"/>

// in the servlet mapped to /getImage.action:
// get the ID of the image from the request parameters
String imageId = request.getParameter("imageId");
byte[] imageData = getImageFromDatabase(imageId);
response.setContentType("image/jpeg");
response.getOutputStream().write(imageData);

All the browsers have a right-click - Save image as... menu item, so I wouldn't implement this in the app.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
4

JSP:

<div id="profileDiv" style="padding: 10px; border: solid 2px #D6D6D6;">
     <img src="imageDisplayProcess.do?pKey=<c:out value="${staff.staffId}" />"
                             width="117" height="160"
                             onError="loadImage()" onAbort="loadImage()" />
</div>

Servlet //imageDisplayProcess

imgByt = imageClass.getPhotograph();//return blob...
response.setContentType("image/jpg");
response.getOutputStream().write(imgByt);
response.getOutputStream().flush();
response.getOutputStream().close();
Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84