Thread: Re: Saving currupting binary data into the database when saving a image into the database

 

Hello,

I’m using hibernate to connect postgresql via java, and I’m trying to save a image as a binary file,

Using the ..

 

When I upload:
..

                        UploadTransferForm uForm = (UploadTransferForm) form;

 

                        int size=uForm.getFileUpload().getFileSize();

                        byte[] byteArr = new byte[size];

 

                        ActionErrors errors = new ActionErrors();

 

 

                        try {

 

                                                //Create an input stream to read the uploaded file.

                                                ByteArrayInputStream bytein = new ByteArrayInputStream(uForm.getFileUpload().getFileData());

                                                // Load the input stream into the byte Array.

                                                bytein.read(byteArr);

                                                // Close the input stream.

                                                bytein.close();

                                                // Load the byte[] into the content field.

                                                uForm.setContent(byteArr);

 

 

                        } catch (Exception e) {

                                    logger.error("Error adding image to the database", e);

                        }                     

 

                                               

                        Session sess = null;

 

                        AuthorityApprovalForms af = new AuthorityApprovalForms();

 

 

                        try {

                                    sess = Waterfind.getSessionFactory().openSession();

                        } catch (net.sf.hibernate.HibernateException e) {

                                    logger.error("Error opening hibernate session", e);

                        }

                                   

                        try {

                                   

                                    af.setImg(byteArr);                              

                                    sess.save(af);

                                    sess.flush();

 

                                   

                        } catch (Exception e) {


..

when i diplaying it

 

try {

 

                                    AuthorityApprovalForms af = new AuthorityApprovalForms();

 

                                    Long formId = new Long(request.getParameter("formId"));

                                   

                                    af = AuthorityApprovalForms.getFormById(formId);

                                   

                                   

                                    byte pic[] = af.getImg();

                                   

                                    OutputStream out = response.getOutputStream();

                                    out.write( pic );

                         } catch (IOException e) {

                                     logger.error("Error opening file region", e);

                         }

 

I have save the data into the database via hibernate-mapping as binary

 

<hibernate-mapping>

    <class name="com.admin.AuthorityApprovalForms" table="AUTHORITY_APPROVAL_FORMS">

        <id name="id" type="long">

            <generator class="native"/>

        </id>                   

                        <many-to-one name="OfferId" class="com.admin.core.Offer">

            <column name="offer_id"/>

        </many-to-one>

                        <property name="name" type="string"/>

                        <property name="type" type="string"/>

                        <property name="img" type="binary"/>

    </class>

</hibernate-mapping>

 

However, when jpg is displaying, it appear to be corrupted, with green and black back ground (similar to missing layers).

I think, when saving the binary data to the database, it try to convert binary data to different format, is there is a way to fix this or any other reason behind it?

 

Can some one please help

 

Regards,

Dilan

Re: Saving currupting binary data into the database when

From
Kris Jurka
Date:

On Tue, 17 May 2005, Dilan wrote:

> I'm using hibernate to connect postgresql via java, and I'm trying to save a
> image as a binary file,
>

Sending this as html email makes it difficult to read.  Please stick to
plain text.  Also it is not clear what hibernate is doing under the hood,
so this may be a question for a hibernate person.

Anyway, older server versions (early 7.3 series) have a bug that corrupts
bytea data, you aren't running one of those are you?

If you can provide a complete example using only JDBC calls I'd be
interested, but I don't know what else I can do for you now.

Kris Jurka

Hello,

I'm using psql 7.3.4-RH,

I'm using hibernate to connect postgresql via java, and I'm trying to save a
image as a binary file, I hope hibernate not corrupting the data connection,
but so far I cant find any article regards that issue, hope the question is
much clear to you now..


When I upload, use this part of the class:


UploadTransferForm uForm = (UploadTransferForm) form;
int size=uForm.getFileUpload().getFileSize();
byte[] byteArr = new byte[size];

ActionErrors errors = new ActionErrors();

try {
    //Create an input stream to read the uploaded file.
    ByteArrayInputStream bytein = new
ByteArrayInputStream(uForm.getFileUpload().getFileData());
    // Load the input stream into the byte Array.
    bytein.read(byteArr);
    // Close the input stream.
    bytein.close();
    // Load the byte[] into the content field.
    uForm.setContent(byteArr);
} catch (Exception e) {
        logger.error("Error adding image to the database", e);
}

Session sess = null;

AuthorityApprovalForms af = new AuthorityApprovalForms();

try {
        sess = Waterfind.getSessionFactory().openSession();
} catch (net.sf.hibernate.HibernateException e) {
        logger.error("Error opening hibernate session", e);
}
   try {
        af.setImg(byteArr);
        sess.save(af);
        sess.flush();

} catch (Exception e) {

--

when i diplaying it



try {

    AuthorityApprovalForms af = new AuthorityApprovalForms();
    Long formId = new Long(request.getParameter("formId"));
    af = AuthorityApprovalForms.getFormById(formId);

    byte pic[] = af.getImg();

    OutputStream out = response.getOutputStream();
    out.write( pic );

     } catch (IOException e) {
             logger.error("Error opening file region", e);
     }
}


I have save the data into the database via hibernate-mapping as binary



<hibernate-mapping>

    <class name="com.admin.AuthorityApprovalForms"
table="AUTHORITY_APPROVAL_FORMS">
        <id name="id" type="long">
            <generator class="native"/>
        </id>
    <many-to-one name="OfferId" class="com.admin.core.Offer">
            <column name="offer_id"/>
        </many-to-one>
    <property name="name" type="string"/>
    <property name="type" type="string"/>
    <property name="img" type="binary"/>

    </class>
</hibernate-mapping>

However, when jpg is displaying, it appear to be corrupted, with green and
black back ground (similar to missing layers).

I think, when saving the binary data to the database, it try to convert
binary data to different format, is there is a way to fix this or any other
reason behind it?



Can some one please help