Spring, Blob download, byte[] size increases twice
I am using Spring 3.0 with Hibernate and PostgreSQL and I 开发者_运维知识库have following problem:
I'm uploading the files to the database, saving its content type, and everything works ok, the size of the field in database is OK. However when I try to download it, Hibernate returns the byte array twice as bigger as it should. The downloaded files are corrupted of course. The size is ESXACTLY 2 times bigger then the size in database... My code looks as follow: The field domain class (with mapping):private byte[] cv;
@Column(name="cv")
public byte[] getCv() {
return this.cv;
}
The DAO function that loads the object:
public Candidate load(Integer id) {
return (Candidate) getHibernateTemplate().get(Candidate.class, id);
}
Session factory config:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="foo.foo.core.domain"/>
</bean>
The SQL Create:
CREATE TABLE candidate
(
id serial NOT NULL,
cv bytea,
...
)
The database is enncoded UTF-8 if it make any difference.
I was trying with org.springframework.jdbc.support.lob.DefaultLobHandler, but it does not do the thing.. Any ideas? It's driving me crazy..found the solution, change your DB setting for bytea output as follows:
ALTER DATABASE SET bytea_output='escape';
Oscar.
I'm having the same issue and still do not know how to solve it but it's for sure a codification problem. I saved both images on disk and the wrong one is setting HEX codes for the char representation of the original codes, i.e
The original image starts with following bytes (HEX representation):
FF D8 FF E0 ...
The revovered byte array (double size) is as follows:
66 66 64 38 66 66 65 30 ...
Which happen to be the HEX codification of "ff d8 ff e0" as chars (f=66HEX)
精彩评论