You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Stefano Tranquillini <st...@gmail.com> on 2009/05/19 18:28:57 UTC

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-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: upload and store img

Posted by Stefano Tranquillini <st...@gmail.com>.
hi,
i've tried with db but is complex, i've do in this way:
using the inteceptior of struts and the tag <s:file in jsp

  private String saveFile(File file, String savePath, String fileName) {
        fileName = fileName.replace(' ', '_');
        File dirPath = new File(savePath);
        if (!dirPath.exists()) {
            dirPath.mkdirs();
        }
        try {
            //retrieve the file data
            InputStream stream = new FileInputStream(file);

            //write the file to the file specified
            OutputStream bos = new FileOutputStream(savePath + File.separator +
                    fileName);
            int bytesRead;
            byte[] buffer = new byte[8192];
            while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            stream.close();
        } catch (Exception e) {
            return null;

        }
        return "img/item/" + fileName;
    }

i simply put in the db the string.
works.

On Tue, May 19, 2009 at 21:13, Martin Gainty <mg...@hotmail.com> wrote:
>
> Struts FileUploadInterceptor offers maximumSize,allowedTypes and allowedExtensions parameters specifically
>
> Interceptor parameters:
>
>
>
>
>
>
>
>
> maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set
>  on the action. Note, this is not related to the various properties found in struts.properties.
>  Default to approximately 2MB.
>
> allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow
>  a file reference to be set on the action. If none is specified allow all types to be uploaded.
>
> allowedExtensions (optional) - a comma separated list of file extensions (ie: .html) that the interceptor will allow
>  a file reference to be set on the action. If none is specified allow all extensions to be uploaded.
> http://struts.apache.org/2.0.14/struts2-core/apidocs/org/apache/struts2/interceptor/FileUploadInterceptor.html
>
> HTH
> Martin Gainty
> ______________________________________________
> Jogi és Bizalmassági kinyilatkoztatás/Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
>  Ez az
> üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy
> jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának
> készítése nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és
> semmiféle jogi alkalmazhatósága sincs.  Mivel az electronikus üzenetek
> könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet
> ezen üzenet tartalma miatt.
>
> Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
> Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.
>
>
>
>
>> Subject: RE: upload and store img
>> Date: Tue, 19 May 2009 13:11:19 -0400
>> From: david_kawczynski@merck.com
>> To: user@struts.apache.org
>>
>> 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.tranquillini@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-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@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-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>
> _________________________________________________________________
> Hotmail® has a new way to see what's up with your friends.
> http://windowslive.com/Tutorial/Hotmail/WhatsNew?ocid=TXT_TAGLM_WL_HM_Tutorial_WhatsNew1_052009



-- 
Stefano

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


RE: upload and store img

Posted by Martin Gainty <mg...@hotmail.com>.
Struts FileUploadInterceptor offers maximumSize,allowedTypes and allowedExtensions parameters specifically

Interceptor parameters:
 

 
 

 

 
maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set
 on the action. Note, this is not related to the various properties found in struts.properties.
 Default to approximately 2MB.
 
allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow
 a file reference to be set on the action. If none is specified allow all types to be uploaded.
 
allowedExtensions (optional) - a comma separated list of file extensions (ie: .html) that the interceptor will allow
 a file reference to be set on the action. If none is specified allow all extensions to be uploaded.
http://struts.apache.org/2.0.14/struts2-core/apidocs/org/apache/struts2/interceptor/FileUploadInterceptor.html

HTH
Martin Gainty 
______________________________________________ 
Jogi és Bizalmassági kinyilatkoztatás/Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 Ez az
üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy
jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának
készítése nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és
semmiféle jogi alkalmazhatósága sincs.  Mivel az electronikus üzenetek
könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet
ezen üzenet tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Subject: RE: upload and store img
> Date: Tue, 19 May 2009 13:11:19 -0400
> From: david_kawczynski@merck.com
> To: user@struts.apache.org
> 
> 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.tranquillini@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-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@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-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

_________________________________________________________________
Hotmail® has a new way to see what's up with your friends.
http://windowslive.com/Tutorial/Hotmail/WhatsNew?ocid=TXT_TAGLM_WL_HM_Tutorial_WhatsNew1_052009

RE: upload and store img

Posted by "Kawczynski, David" <da...@merck.com>.
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.tranquillini@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-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@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-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org