开发者社区> 问答> 正文

java获取oracle数据库blob图片字节流显示到jsp页面出错bogus ?报错

最近在做项目时需要从oracle读取blob图片列表,显示到jsp页面上。使用以下代码实现,大部分图片显示正常,但是有些图片执行BufferedImage bis = 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;
}


展开
收起
爱吃鱼的程序员 2020-06-22 14:37:08 805 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    异常栈贴出来异常贴全店

    2020-06-22 14:37:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PostgresChina2018_樊文凯_ORACLE数据库和应用异构迁移最佳实践 立即下载
PostgresChina2018_王帅_从Oracle到PostgreSQL的数据迁移 立即下载
Oracle云上最佳实践 立即下载