将Oracle中Blob字段存储的图片转换成字节流直接输出到浏览器

时间:2022-10-21 12:59:06

数据库中blob字段存储的图片,通过Controller输出到浏览器客户端并显示出来。

实现方式:SpringMVC,关键部分代码片段如下:

1,编写铺助类及函数 

package com.acc.sp.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import oracle.sql.CLOB;
 /*
 * Author WangSl
 * Version 1.0
 * Aug 29, 2012 2:38:05 PM
 */
public class StringUtil {
	//数组转换成","分隔的连接数据
	public static String getFormItemValue(Object obj) {
		String ret = "";
		if (obj == null) {
			ret = "";
		} else if (obj instanceof String) {
			ret = obj.toString();
		} else if (obj instanceof String[]) {
			String arr[] = (String[]) obj;
			for (int i = 0; i < arr.length; i++) {
				ret += arr[i] + ",";
			}
			if (ret.endsWith(",")) {
				ret = ret.substring(0, ret.length() - 1);
			}
		}
		return ret;
	}
	//","分隔后的数据 添加单引号
	public static String getOneSysbolFormItemValue(Object obj) {
		String ret = "";
		String otRet = "";
		if (obj == null) {
			ret = "";
		} else if (obj instanceof String) {
			ret = obj.toString();
			if(ret.indexOf(",") > -1){
				String arr[] = ret.split(",");
				for (int i = 0; i < arr.length; i++) {
					otRet += "'"+arr[i] + "',";
				}
				ret = otRet;
			}
		} else if (obj instanceof String[]) {
			String arr[] = (String[]) obj;
			for (int i = 0; i < arr.length; i++) {
				ret += "'"+arr[i] + "',";
			}
			
		}
		if (ret.endsWith(",")) {
			ret = ret.substring(0, ret.length() - 1);
		}
		return ret;
	}
	//将Map中的Clob列数据转换成String,并返回String
	public String getClob2String(Map map, String column) {
		String str = "";
		Object clob = map.get(column);
		str = this.getClob2String(clob);
		return str;
	}
    
	//将Map中的Clob列数据转换成String,重新存入到Map中,并返回Map
	public Map getClob2StringInMap(Map map, String column) {
		String str = "";
		Object clob = map.get(column);
		str = this.getClob2String(clob);
		map.remove(column);
		map.put(column, str);
		return map;
	}
    
	//clob 转换 String (传入 clob 类型的 obj 当参数)
	public static String getClob2String(Object obj) {
		String str = "";
		try {
			CLOB clob = (CLOB) obj;
			if (clob == null || clob.length() == 0) {
				str = "";
			} else {
				long clen = clob.length();
				char clobArray[] = new char[(int) clen];
				int readednum = clob.getChars(1, (int) clen, clobArray);
				StringBuffer sb = new StringBuffer();
				sb.append(clobArray);
				str = sb.toString();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	//  输出 blob  字段图片  (传入 blob 类型的 obj 当参数)
	 public static void outPutBlobImg(Object obj,HttpServletResponse response) {
		Blob imgBlob = null;
		OutputStream out=null;
		InputStream fis = null;
		try {
			imgBlob = (Blob)obj;
			if(obj==null || imgBlob.length()==0){		
				return ;
			}
			out = response.getOutputStream();
			fis = imgBlob.getBinaryStream();
			for (int b = fis.read(); b != -1; b = fis.read()) {
				out.write(b);
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				fis.close();
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}		
		
	}
	 public static void main(String[] args) {
		System.out.println( getOneSysbolFormItemValue("a,"));
	}
}
2,显示图片的Controller对应的方法
// 显示图片信息
@RequestMapping(value = "/imgstrb64")
public void getImgBase64StrById(HttpServletRequest request, HttpServletResponse response) throws IOException {
	response.setContentType("text/text;charset=UTF-8");
	response.setHeader("Cache-Control", "no-cache");
	PrintWriter out =response.getWriter();		
	Map param = this.getParameter(request);
	List list = this.spJobService.findImgById(param);
	String imgStr = "";		
	if (null != list && !list.isEmpty()) {
		Iterator ite = list.iterator();
		if (ite.hasNext()) {
			Map map = (Map) ite.next();
			if (map != null && !map.isEmpty()) {
				if (map.get("IMG") != null) {
					imgStr = com.acc.sp.util.StringUtil.getClob2String(map.get("IMG"));						
				}
			}
		}
	}
    out.print(imgStr);
    out.flush();
    out.close();
}

3,输入imgstrb64访问路径测试