如何从数据库中查看jsp页面中的多个图像? [英] How to view more than one image in a jsp page from database?

查看:57
本文介绍了如何从数据库中查看jsp页面中的多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试从db获取图像然后在JSP页面中显示图像时遇到问题:

I have problem trying to fetch images from db and then showing them in a JSP page:

ImageAction:

ImageAction:

public class ImageAction {

private  byte[] itemImage;

public byte[] getItemImage() {
    return itemImage;
}

public void setItemImage(byte[] itemImage) {
    this.itemImage = itemImage;
}

public  void  execute() throws Exception{
      try {
          HttpServletResponse response = ServletActionContext.getResponse();
          response.reset();
          response.setContentType("multipart/form-data"); 
          byte[] imgData =(byte[])ServletActionContext.getRequest().getSession()
                           .getAttribute("imageData"); 
          System.out.println("imgData :: "+imgData);
          itemImage = imgData;
          ServletActionContext.getRequest().getSession().removeAttribute("imageData") ; 
          OutputStream out = response.getOutputStream();
          out.write(itemImage);
          out.flush();
          out.close();

      } catch (Exception e) {
          System.out.println("error :: ");
          e.printStackTrace();
      }
   //   return "success";
  }
}

jsp:

<tr >
    <td> <%= map.get(mapKey) %> </td>
    <td colspan="1" >
        <img src="<s:url value="ImageAction" />" width="115" border="0" />
    </td>   
</tr>

推荐答案

您将在JSP页面上收到的内容是原始的byte[],您需要对其进行一些处理,请检查以下内容:

What you'll receive on your JSP page would be raw byte[], you need to process it a little, check following:

<a src="" id="imageSrc"/>

一个简单的anchor标签以显示图像.

A simple anchor tag to display image.

以下代码会将原始的byte[]转换为等效的Base64字符串,即显示图像必不可少.

Following code would convert the raw byte[] to it's equivalent Base64 string, viz is essential to display image.

<%
   byte data[]=request.getParameter(itemImage); //let itemImage for instance hold your data
   String encodedData=Base64.encodeBytes(data);
%>

现在,您需要在JSP页面上使用一些实用程序来设置base64数据

Now you need a little utility on your JSP page to set base64 data

<script>
function imageBaseSixtyFourProcessor(baseSixtyFour){
    var img = new Image();
    img.src='';
        var imageUrl='data:image/gif;base64,'+baseSixtyFour;
        $('#imageSrc').attr('src',imageUrl);
        img.src = imageUrl;
    }
}
</script>

调用上述函数:

<script>
  imageBaseSixtyFourProcessor(<%encodedData%>);
</script>

注意:在我的情况下,我正在显示GIF文件,您可以根据需要进行更改.

Note : In my case I am displaying GIF file, you can change it as per your requirements.

类似地,您可以用相同的方式显示多张图像.

Similarly you can have multiple images displayed in the same way.

这篇关于如何从数据库中查看jsp页面中的多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆