javaphppostgresqlbyteajdbc-postgres

How do I insert a JPEG image into a PostgreSQL bytea field using Java and then display on a website using PHP?


I made a little Java program that reads image files (jpg/jpeg) and inserts them into a database table covers.cover, which is of type bytea.

I'm fairly certain that the byte[] cover parameter that is passed to the Java addCover(int riddim_id, byte[] cover, byte[] thumbnail) method contains valid jpeg data (I've tested by writing it to a .jpeg file, which displayed fine).

The database function add_cover(riddim INT, cover BYTEA, thumbnail BYTEA) appears to be called correctly; after a call there is data in the table and I do not get any SQL errors.

However, the data looks somewhat like

\377\330\377\340\000\020JFIF\000\001\001\001\001,\001,\000\000\377\333\000C\000\013\010\010\010\011\010\014\011\011\014\021\013\012\013\021\024\017\014\014\017\024\027\022\022\022\022\022\027\030\023\024\024\024\024\023\030\026\032\033\034\033\032\026""$$"".....0000000000\377\333\000C\001\014\014\014\020\020\020\027\021\021\027\030\024\023\024\030\036\033\034\034\033\036$\036\036\037\036\036$)#    #)&($$$(&++))++000000000000000\377\300\000\021\010\005\200\005\230\003\001\021\000\002\021\001\003\021\001\377\304\000\033\000\000\003\000\003\001\001\000\000\000\000\000\000\000\000\000\000\000\001\002\003\004\006\005\007\377\304\000U\020\000\001\002\004\003\005\004\010\003\006\005\003\003\001\001\021\001\002\021\000!1A\003\022Q\004"2Ba\005\023bq\006CRr\201\221\241\3603\261\301\024#c\202\321

and so on, it doesn't look like valid data to me. I expected something more uniform (less special characters like ,, " and $), something like \x01E25A43.

When I go to http://foo.bar/image.php, which is the PHP script shown below, Firefox tells me that the image can not be displayed because it contains erroneous data.

I assume I am not correctly using the BYTEA field but I can't figure out what I am doing wrong. Any suggestions?

The database table looks like:

CREATE TABLE covers (
    cover_id              SERIAL      PRIMARY KEY,
    riddim_id             SERIAL      UNIQUE REFERENCES riddims (riddim_id),
    coverhash             VARCHAR(32) NOT NULL UNIQUE,
    cover                 BYTEA       NOT NULL,
    thumbnail             BYTEA       NOT NULL
);

The database function that I am calling with java looks like:

CREATE OR REPLACE FUNCTION add_cover(_riddim_id INT, _cover BYTEA, _thumbnail BYTEA) RETURNS INTEGER AS $$
DECLARE
    _cover_id INT;
BEGIN
    SELECT cover_id INTO _cover_id FROM covers WHERE riddim_id = _riddim_id;

    IF (_cover_id IS NULL) THEN
        INSERT INTO covers (riddim_id, coverhash, cover, thumbnail) VALUES (
            _riddim_id,
            md5(_cover),
            _cover,
            _thumbnail
        ) RETURNING cover_id INTO _cover_id;
    END IF;

    RETURN _cover_id;
END;
$$ LANGUAGE plpgsql;

The Java method that calls the database function above using JDBC connector looks like:

private int addCover(int riddim_id, byte[] cover, byte[] thumbnail) 
        throws SQLException {    
    int cover_id;

    try (CallableStatement cs = conn.prepareCall("{ ? = call add_cover(?, ?, ?) }")) {
        cs.registerOutParameter(1, Types.INTEGER);
        cs.setInt(2, riddim_id);
        cs.setBytes(3, cover);
        cs.setBytes(4, thumbnail);
        cs.execute();
        cover_id = cs.getInt(1);
    }

    return (cover_id != 0) ? cover_id : -1;
}

The PHP script (that I found somewhere on Google) that sends the data as image/jpeg to the browser:

<?php
  // Connect to the database
  $dbconn = pg_connect("<censored>");

  // Get the bytea data
  $res = pg_query("SELECT cover FROM covers WHERE cover_id = 11");
  $raw = pg_fetch_result($res, 'cover');

  // Convert to binary and send to the browser
  header('Content-type: image/jpeg');
  echo pg_unescape_bytea($raw);
?>

I'm running PostgreSQL version 9.4.4, using postgresql-9.4-1202.jdbc41.jar.


Solution

  • The query failed without giving an error message (more accurately the query always returned an empty resultset) because I was using pg_connect("host=foo.bar ...") but pg_hba.conf was not configured to allow external connections sent from localhost, apparently. Changing host=foo.bar to host=localhost solved the problem.

    I'm not quite sure why $dbconn returned true, I figured out that something went wrong with the connection when checking /var/log/postgresql/postgresql-9.4-main.log which told me that the user I was trying to connect with had no valid settings for the specified host in pg_hba.conf.