从数据库中获取后更改了图像颜色

时间:2018-03-20 08:41:18

标签: java jdbc

我在使用Java从数据库中获取图像时遇到问题。我写了这个函数:

public byte[] fetchPhoto(Long id) throws SQLException {
        Connection conn = DataSourceUtils.getConnection(dataSource);
        byte[] photo = null;
        Blob imageBlob = null;
        try (ResultSet rs = conn.createStatement()
                .executeQuery("select IMAGE from IMAGE_TABLE where IMAGE_ID = " + id)) {

            while (rs.next()) {
                imageBlob = rs.getBlob(1);
                photo = imageBlob.getBytes(1, (int) imageBlob.length());
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            DataSourceUtils.releaseConnection(conn, dataSource);
        }
        return photo;
    }

好的,我已经获取了图像,但效果如下: this

原始图像 here

我不知道为什么拿到后我有这种颜色。我在数据库中检查了这个图像,所以这里没问题。

1 个答案:

答案 0 :(得分:0)

也许您需要像here这样的解决方案。所以使用这个例子,这是你改进的方法:

select 
m.messageid,    
m.message,
m.orig,     
m.recip, 
d1.company as orig_company,
d1.department as orig_department,
d1.office as orig_office,
d1.country as orig_country,
d2.company as recip_company,
d2.department as recip_department,
d2.office as recip_office,
d2.country as recip_country
from messages m
left join department d1
    on m.originator = d1.address
left join department d2
    on m.recip = d2.address

如果我是你,我将此代码移至separete方法。