0

Firstly, I would like to say that I know images should really be stored in a database as a BLOB...

I am working with a system where the image has been stored in the database as a CLOB. I just have to work with what I have been given...Hence my question to SO.

Here is what I have done so far...

Query the database (using Hibernate), I can successfully obtain a single record and all of its fields. I am reading the CLOB column into a char array:

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

Next, I have assumed that in order to store the image in a CLOB, it must have been Base64 encoded. So, I convert my char array to a String:

String base64DataString = new String(podSignatureImage);

This means that I should then be able to do the following in my jsp:

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

Except that this hasn't worked. I don't get any errors. I can see my jsp page, but I cannot see the image.

I would be very grateful for some advice.

Thank you.

user2318704
  • 95
  • 2
  • 9

2 Answers2

0

please try this to encode to base64 :

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

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

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

I hope it will help you

0

can you provide a CLOB String?

there is a function convert base64Str to 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);
    }
  }

you can try this for test.

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;
  }
}
hisenyuan
  • 9
  • 4