Retrieving from DB and outputting to HTTP:
Every time I've done this it has been without the use of struts.  I use
a
servlet that looks for e request parameter storing an ID that resolves
to
the image in the DB.  I lookup the image, set the response type for that

image, then output the bytes that make up that image directly to the
response.  
Say the servlet is mapped to "/GetImageServlet".  You can then use that
path
As the SRC of an image as follows:
        <IMG SRC="/GetImageServlet" />


Inserting the image from http request to DB:
I've done this with mysql and oracle, but am not too familiar with
postgres.


For this to work with fileuploadinterceptor, the POJO must have 3
properties:
        public class Example {
                ...
                File upload;
                String uploadFileName;
                String uploadContentType;
                ...
        }


I use hibernate to put it in the DB.  Here's how I did it with Oracle.  
You can probably modify for this as necessary Postgres. For this to work
with 
Hibernate, the POJO must have 1 encapsulated property:
        public class Example {
                ...
                Blob attachment;
                ...
        }


This is the getter for that property:   
        public class Example {
                ...
                /**
                 * Returns a {...@link Blob} equivalent of this object's 
                 * {...@link #upload}.  Do not call this directly -- it 
                 * it intended for Hibernate interaction with Oracle
ONLY.
                 *  
                 * @return
                 *         a {...@link Blob} equivalent of this object's 
                 *         {...@link #attachmentByteStream}.
                 */
                public Blob getAttachment() {
                        try {
                                if(this.attachment != null) {
                                        return this.attachment;
                                }
                                else {
                                        if(this.upload != null) {
                                                return
Hibernate.createBlob(
                                                        new
FileInputStream(upload));
                                        }
                                        else return null;
                                }
                        }
                        catch (IOException ioException) {
                                ioException.printStackTrace();
                                return null;
                        }
                }
                ...
        }


Additionally, there are some other setters/getters Hibernate/Oracle
requires,
the base of their name must match the name declared in the hbm.xml file:
        public class Example {
                ...
                /**
                 * Sets this item's {...@link #attachment} {...@link Blob}.
Do not call this  
                 * directly -- it intended for Hibernate interaction
with Oracle ONLY.
                 * 
                 * @param attachment
                 *        the {...@link Blob} to set to this object's
{...@link #attachment}.
                 */
                public void setAttachment(Blob attachment) {
                        this.attachment = attachment;
                }
        
                public InputStream getAttachmentStream() throws
SQLException {
                        return (attachment==null ? null :
attachment.getBinaryStream());
                }

                public void setAttachmentStream(InputStream
attachmentStream) 
                throws IOException {
                        this.attachment =
Hibernate.createBlob(attachmentStream);
                }
                ...
        }


This is the relevant part of the hibernate mapping:
        <hibernate-mapping>
                <class name="com.foo.Example" table="EXAMPLE">
                        <property name="attachment" type="blob">
                                <column name="ATCHMNT_LOB" />
                        </property>
                </class>
        </hibernate-mapping>


This is the relevant fragment from the html form submitting to struts:
        <s:form action="save" 
                        name="requestform" 
                        method="post" 
                        enctype="multipart/form-data">
                <s:file name="siteRequest.upload" 
                                label="Attachment" />
        </s:form>

-----Original Message-----
From: Stefano Tranquillini [mailto:stefano.tranquill...@gmail.com] 
Sent: Tuesday, May 19, 2009 12:29 PM
To: Struts Users Mailing List
Subject: UI: upload and store img

Hi all.
i've to do a UI that permits to store some images, how can i do that?
i tried to use upload file but is quite complex and doesn't work.
is better if i put the img inside the db?
someone of u has experience about that?
if i put inside the db the img (i use postrgres) how can i display it in
a jsp?

thanks folks

-- 
Stefano

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or
MSD and in Japan, as Banyu - direct contact information for affiliates is
available at http://www.merck.com/contact/contacts.html) that may be
confidential, proprietary copyrighted and/or legally privileged. It is
intended solely for the use of the individual or entity named on this
message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and
then delete it from your system.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to