javax.imageio.IIOException:不是JPEG流(以:0x0003开头,预期SOI:0xffd8)

时间:2017-08-22 09:52:55

标签: java jdbc javax.imageio

将inputStream转换为bufferImage时遇到问题。 BufferedImage crfPersonImg始终为null,并且在行 Raster raster = reader1.readRaster(0,null); 中发生异常。到目前为止,这是我的代码

//code for getting ByteArray From Blob column in Oracle DB.
    List<Object[]> userRoleList = commonDao.findByNativeSQL(selectSQL
            , new Object[] {examCd, examPeriod, fromMemNo, toMemNo});
    for(Object[] row : userRoleList){
        CertificateDtlsVO cdObj = new CertificateDtlsVO();          
        cdObj.setExmCd(Integer.parseInt(row[0].toString()));
        cdObj.setExmDsc(row[1].toString());
        cdObj.setMemNo(row[2].toString());
        cdObj.setMemNm(row[3].toString());
        cdObj.setCertNo(row[4].toString());
        cdObj.setCertDt(row[5].toString());
        cdObj.setPrtNo(row[6].toString());
        cdObj.setSylCd(row[7].toString());
        cdObj.setNumOfTime(Integer.parseInt(row[8].toString()));
        cdObj.setMemType(row[9].toString());
        cdObj.setClassGrade(row[10].toString());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(row[11]);           
        cdObj.setPhoto(baos.toByteArray());
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(row[12]);
        cdObj.setSign(baos.toByteArray());
        cdObj.setPhotoBuffImg(ImageIO.read(new ByteArrayInputStream(baos.toByteArray())));
        oos.flush();
        oos.close();
        memCertfDtlsList.add(cdObj);
    }

//代码用于将字节数组转换为BufferImage

InputStream in = new ByteArrayInputStream(photo);       
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
    ImageReader reader1 = null;
    while (readers.hasNext()) {
        reader1 = readers.next();
        if (reader1.canReadRaster()) {
            break;
        }
    }

    //Stream the image file
    ImageInputStream input =   ImageIO.createImageInputStream(in); 
    reader1.setInput(input); 

    //Read the image raster
    Raster raster = reader1.readRaster(0, null); 

    //Create a new RGB image
    BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(), 
            BufferedImage.TYPE_4BYTE_ABGR); 

    //Fill the new image with the old raster
    bi.getRaster().setRect(raster);

    BufferedImage crfPersonImg = ImageIO.read(in);
  

javax.imageio.IIOException:不是JPEG流(以:0x0003开头,预期SOI:0xffd8)       在com.twelvemonkeys.imageio.plugins.jpeg.JPEGSegmentImageInputStream.streamInit(JPEGSegmentImageInputStream.java:247)       at com.twelvemonkeys.imageio.plugins.jpeg.JPEGSegmentImageInputStream.fetchSegment(JPEGSegmentImageInputStream.java:71)       在com.twelvemonkeys.imageio.plugins.jpeg.JPEGSegmentImageInputStream.repositionAsNecessary(JPEGSegmentImageInputStream.java:267)

示例图片sample Image

1 个答案:

答案 0 :(得分:0)

感谢@haraldK,通过更改从blob列获取字节数组的代码解决了我的问题。

List<Object[]> userRoleList = commonDao.findByNativeSQL(selectSQL
            , new Object[] {examCd, examPeriod, fromMemNo, toMemNo});
    for(Object[] row : userRoleList){
        CertificateDtlsVO cdObj = new CertificateDtlsVO();          
        cdObj.setExmCd(Integer.parseInt(row[0].toString()));
        cdObj.setExmDsc(row[1].toString());
        cdObj.setMemNo(row[2].toString());
        cdObj.setMemNm(row[3].toString());
        cdObj.setCertNo(row[4].toString());
        cdObj.setCertDt(row[5].toString());
        cdObj.setPrtNo(row[6].toString());
        cdObj.setSylCd(row[7].toString());
        cdObj.setNumOfTime(Integer.parseInt(row[8].toString()));
        cdObj.setMemType(row[9].toString());
        cdObj.setClassGrade(row[10].toString());
        InputStream photoBlob = ((Blob) row[11]).getBinaryStream();
        InputStream signBlob = ((Blob) row[12]).getBinaryStream();
        cdObj.setPhoto(IOUtils.toByteArray(photoBlob));
        cdObj.setSign(IOUtils.toByteArray(signBlob));
        memCertfDtlsList.add(cdObj);
    }