Spring,Blob下载,byte[]大小增加两倍

发布于 2024-10-18 11:09:55 字数 1405 浏览 1 评论 0原文

我将 Spring 3.0 与 Hibernate 和 PostgreSQL 一起使用,但遇到以下问题:
我正在将文件上传到数据库,保存其内容类型,一切正常,数据库中字段的大小也正常。然而,当我尝试下载它时,Hibernate 返回的字节数组是应有的两倍。下载的文件当然已损坏。大小是数据库中大小的 ESXACTLY 2 倍...我的代码如下所示:
字段域类(带映射):

private byte[] cv;

@Column(name="cv")
public byte[] getCv() {
  return this.cv;
}

加载对象的 DAO 函数:

public Candidate load(Integer id) {
  return (Candidate) getHibernateTemplate().get(Candidate.class, id);
}

会话工厂配置:

<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>

SQL 创建:

CREATE TABLE candidate
(
  id serial NOT NULL,
  cv bytea,
  ...
)

数据库采用 UTF-8 编码(如果有任何区别)。
我正在尝试使用 org.springframework.jdbc.support.lob.DefaultLobHandler,但它不起作用..有什么想法吗?这让我发疯..

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仲春光 2024-10-25 11:09:55

找到解决方案,更改 bytea 输出的数据库设置,如下所示:

ALTER DATABASE SET bytea_output='escape';

奥斯卡.

found the solution, change your DB setting for bytea output as follows:

ALTER DATABASE SET bytea_output='escape';

Oscar.

只为一人 2024-10-25 11:09:55

我遇到了同样的问题,仍然不知道如何解决,但这肯定是一个编码问题。
我将两个图像都保存在磁盘上,错误的是为原始代码的字符表示设置了十六进制代码,即

原始图像以以下字节开头(十六进制表示):

FF D8 FF E0 ...

反转的字节数组(双倍大小) )如下:

66 66 64 38 66 66 65 30 ...

这恰好是“ff d8 ff e0”作为字符的十六进制编码(f=66HEX)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文