How to display an image in jsp?

前端 未结 2 1105
悲&欢浪女
悲&欢浪女 2020-11-30 12:05

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

相关标签:
2条回答
  • 2020-11-30 12:36

    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();
    
    0 讨论(0)
  • 2020-11-30 12:53

    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.

    0 讨论(0)
提交回复
热议问题