如何在jsp中显示已存储为CLOB的图像

时间:2017-05-11 10:34:50

标签: java hibernate jsp clob

首先,我想说我知道图像应该真正存储在数据库中作为BLOB ......

我正在使用一个系统,其中图像已作为CLOB存储在数据库中。我只需要处理我所得到的......因此我的问题就是这样。

这是我到目前为止所做的......

查询数据库(使用Hibernate),我可以成功获取单个记录及其所有字段。我正在将CLOB列读入char数组:

@Column(name="POD_SIGNATURE_IMG", nullable=true)
@Lob
@Basic(fetch=FetchType.LAZY)
private char[] podSignatureImage;

接下来,我假设为了将图像存储在CLOB中,它必须是Base64编码的。所以,我将我的char数组转换为String:

String base64DataString = new String(podSignatureImage);

这意味着我应该能够在我的jsp中执行以下操作:

<img alt="image" src="data:image/jpg;base64,${model.base64DataString}">

除此之外没有效果。我没有收到任何错误。我可以看到我的jsp页面,但我看不到图像。

我会非常感谢你的一些建议。

谢谢。

2 个答案:

答案 0 :(得分:0)

请尝试将其编码为base64:

byte[] encodeBase64 = new String(podSignatureImage).getBytes();

        String base64DataString = org.apache.commons.codec.binary.Base64.encodeBase64(encodeBase64);

        base64DataString   = new String(encodeBase64, "UTF-8");

我希望它能帮到你

答案 1 :(得分:0)

你能提供CLOB字符串吗?

有一个函数将base64Str转换为file。

public static void bytes2File(byte[] bytes, String filePath) {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    File file = null;
    try {
      file = new File(filePath);
      BuildFileUtil.buildFile(file);
      file = new File(filePath);
      BuildFileUtil.buildFile(file);
      fos = new FileOutputStream(file);
      bos = new BufferedOutputStream(fos);
      bos.write(bytes);
      bos.flush();
      System.out.println("生成完成");
    } catch (Exception e) {
      e.printStackTrace();
      logger.error("错误:"+e);
    } finally {
      IOUtils.closeQuietly(bos,fos);
    }
  }

您可以尝试进行测试。

package com.hisen.image;

import com.hisen.utils.Base64Util;
import com.hisen.utils.File2ByteArraysUtil;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import sun.misc.BASE64Encoder;

/**
 * <img src="data:image/png;base64,<%=imageStr%>" alt="base64image"/>
 * Created by hisenyuan on 2017/5/11 at 18:44.
 */
public class ShowImageByBase64 {

  public static String showimage() {
    //写相对路径会报错,暂时不知道如何解决
    String imagePath = "C:\\1\\830.jpg";
    byte[] bytes = File2ByteArraysUtil.file2Bytes(imagePath);
    String s = Base64Util.encodeBase64(bytes);
    return s;
  }

  /**
   * sun.misc.BASE64Encoder
   */
  public static String encodeBase64(byte[] str) {
    if (str == null) {
      return null;
    } else {
      BASE64Encoder encoder = new BASE64Encoder();
      try {
        return encoder.encode(str);
      } catch (Exception var3) {
        return null;
      }
    }
  }

  /***
   * file2byte[]
   * @param path
   * @return
   */
  public static byte[] file2Bytes(String path) {
    byte[] buffer = null;
    File file = new File(path);
    try {
      FileInputStream fis = new FileInputStream(file);
      ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
      byte[] b = new byte[1000];
      int n;
      while ((n = fis.read(b)) != -1) {
        bos.write(b, 0, n);
      }
      fis.close();
      bos.close();
      buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return buffer;
  }
}