简体   繁体   English

如何在jsp中显示图像?

[英]How to display an image in jsp?

I have a bytearray image. 我有一个bytearray图像。

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: 我需要在jsp页面中以jpg格式显示该图像,并且在单击图像时,我可以将图像下载到我的电脑:

I am loading the image from my mysql db as byte array.. 我从我的mysql数据库加载图像作为字节数组..

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 我需要在jsp中显示图像

eg: image.jpg image2.jpg 例如:image.jpg image2.jpg

and by clicking image.jsp , i can download that image and save it to my pc 通过单击image.jsp,我可以下载该图像并将其保存到我的电脑

Please help 请帮忙

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. 您在JSP中生成的HTML必须包含一个img元素,其中src指向servlet或action的URL,该URL将从数据库加载图像并将其发送到具有image / jpeg内容类型的输出流。

// 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. 所有浏览器都右键单击 - 将图像另存为...菜单项,因此我不会在应用程序中实现此功能。

JSP: 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 Servlet // imageDisplayProcess

imgByt = imageClass.getPhotograph();//return blob...
response.setContentType("image/jpg");
response.getOutputStream().write(imgByt);
response.getOutputStream().flush();
response.getOutputStream().close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM