Skip to content
Advertisement

Size of byte array gets double when it retrieve from Postgres database

I am using JSF2.0, jetty server and Postgres database. I want to store mp3 file in database in byte array formet. I upload the byte array of mp3 file. But when I retrive same byte array from database I get byte array of double size and my because of that my mp3 file is not working as well.

JavaScript

Output:

Upload byte array size:384440

Retrived byte array size:768879

I dont know why it is happening. I am storing byte array and on the next line I am retriving it. But I get byte array of double size. Let me know if you need more details.

Advertisement

Answer

When dealing with binary data in Postgres, it is important to use proper data binding.

First, data column must be of type BYTEA. Is that the case?

To store data, use prepared statements and be sure to call setBinaryStream():

JavaScript

To retrieve data, also use prepared statements and use getBytes():

JavaScript

See more here.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement