java查询blob字段出错_java获取oracle数据库blob图片字节流显示到jsp页面出错bogus marker length...

最近在做项目时需要从oracle读取blob图片列表,显示到jsp页面上。使用以下代码实现,大部分图片显示正常,但是有些图片执行BufferedImagebis = ImageIO.read(is);会报错。求高手指教,这个问题该怎么解决?

报错信息如下:

javax.imageio.IIOException: bogus marker length

主要代码如下:

ps = con.prepareStatement(sql);

rs = ps.executeQuery();

if (rs.next()) {

blob = rs.getBlob(field[1]);

if(blob!=null){

inStream = blob.getBinaryStream();

long nLen = blob.length();

int nSize = (int) nLen;

data = new byte[nSize];

inStream.read(data);

data = changeImgSize(data, width, height);

}

}

/**

*

* 缩小或放大图片

* @param data 图片的byte数据

* @param w 需要缩到的宽度

* @param h 需要缩到高度

* @return 缩放后的图片

*/

public static byte[] changeImgSize(byte[] data, int nw, int nh) {

byte[] newdata = null;

BufferedImage bis = null;

ByteArrayOutputStream baos = null;

try {

InputStream is = new ByteArrayInputStream(data);

bis = ImageIO.read(is);

int w = bis.getWidth();

int h = bis.getHeight();

double sx = (double) nw / w;

double sy = (double) nh / h;

AffineTransform transform = new AffineTransform();

transform.setToScale(sx, sy);

AffineTransformOp ato = new AffineTransformOp(transform, null);

// 原始颜色

BufferedImage bid = new BufferedImage(nw, nh,

BufferedImage.TYPE_3BYTE_BGR);

ato.filter(bis, bid);

// 转换成byte字节

baos = new ByteArrayOutputStream();

ImageIO.write(bid, "jpeg", baos);

newdata = baos.toByteArray();

} catch (Exception e) {

e.printStackTrace();

} finally{

if(null != bis){

bis = null;

}

try {

baos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return newdata;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值