Getting nearly double the length when reading byte[] from postgres with jpa

后端 未结 5 724
夕颜
夕颜 2021-01-06 11:50

I have an Image class that has a byte[] to contain the actual image data. I\'m able to upload and insert the image just fine in my webapp. When I attempt to display the im

相关标签:
5条回答
  • 2021-01-06 12:14

    Try looking at the data you're getting. It may give you a clue as to what's happening.

    0 讨论(0)
  • 2021-01-06 12:16

    In PostgreSQL 9 byte[] is sent to client using hex encoding.

    If this is reason for error you have to find update for JPA. Or you may change config of DB server but previous is better.

    0 讨论(0)
  • 2021-01-06 12:17

    Check whether you have an old postgresql jar. I faced the same problem, and found both 8.3 postgresql jar and a 9.1 postgresql jar in my lib. After remove 8.3 postgresql, byte[] works fine.

    0 讨论(0)
  • 2021-01-06 12:34

    The "bytea_output = escape" is just a workaround, Postgres 8.0 changed the bytea encoding to hex.

    Use a current JDBC driver since 9.0-dev800 (9.0 Build 801 is up-to-date currently) and the problem will be solved.

    0 讨论(0)
  • 2021-01-06 12:34

    Supplementary answer for GlassFish 3.x users (principles may apply to other app servers)

    You may be inadvertently using an old PostgreSQL JDBC driver. You can test this by injecting a DataSource somewhere (e.g. an EJB) and executing the following on it:

    System.out.println(ds.getConnection().getMetaData().getDriverVersion());

    In my case, it was 8.3 which was unexpected since I was deploying with 9.1 drivers.

    To find out where this was coming from:

    System.out.println(Class.forName("org.postgresql.Driver").getProtectionDomain().getCodeSource().getLocation());

    As it turned out for me, it was in the lib directory of my GlassFish domain. I'm not sure how it got there - GlassFish certainly doesn't ship that way - so I just removed it and the problem went away.

    0 讨论(0)
提交回复
热议问题