[jboss-user] [JBoss Seam] - Re: File upload to application folder

gmarcus do-not-reply at jboss.com
Wed Aug 1 14:30:39 EDT 2007


Seam 1.2.1GA
Jboss 4.0.5

I think the challenge here is not receiving the file, but then being able to write the file to a specified location.

Specifically, I wish to allow a user to upload a .jpg or .png and have it saved off my webroot so that the image can be displayed using an image tag.  I do not want to save these images into the database as seamspace and ui does.  I want to put them into the filesystem.

I currently have s:fileupload configured and working.  The filecontents are available to my SFSB as 


  | private byte[] picture
  | 

Even if it was an InputStream, I still need to be able to write it out to a new file off of webroot.
Here is what I have so far:

profile.xhtml

  |             <s:fragment rendered="#{user.picture ne null}">
  | 				<s:div id="memberPicture">					      
  | 					<h:outputLabel for="picture">Member photo:</h:outputLabel>
  | 					<h:outputText value="#{user.picture.originalFilename}" />
  | 				  	<h:graphicImage value="/content/#{user.picture.filename}"/>
  | 				</s:div>
  |            	</s:fragment>
  | 			<s:fragment rendered="#{identity.loggedIn and (loggedInUser.id==user.id)}">
  | 	            <s:fragment rendered="#{user.picture==null}">
  | 					<h:form enctype="multipart/form-data">
  | 					  <s:validateAll>
  | 					    <div class="formRow">
  | 					      <h:outputLabel for="picture">Member photo</h:outputLabel>
  | 					      <s:fileUpload id="picture" data="#{profile.picture}" accept="images/png,images/jpg" contentType="#{profile.pictureContentType}" fileName="#{profile.pictureOriginalFilename}"/>
  | 					      <div class="validationError"><h:message for="picture"/></div>
  | 					    </div>               
  | 					  </s:validateAll>
  | 					  <div class="buttons">
  | 					    <h:commandButton value="Upload" action="#{profile.addPicture}" />
  | 					  </div>
  | 					</h:form>
  | 	           	</s:fragment>
  | 	            <s:fragment rendered="#{user.picture ne null}">
  | 					<h:form enctype="multipart/form-data">
  | 					  <div class="buttons">
  | 					    <h:commandButton value="Delete" action="#{profile.deletePicture}" />
  | 					  </div>
  | 					</h:form>
  | 	           	</s:fragment>
  |             </s:fragment>
  | 

ProfileAction.java (the SFSB)

  | @Stateful
  | @Name("profile")
  | public class ProfileAction implements Profile
  | {
  | 	@In(required = false, create = false)
  | 	@Out(required = false, scope=SESSION)
  | 	private User loggedInUser;
  | 	
  | 	private String pictureOriginalFilename;
  | 	private String pictureContentType;
  | 	private byte[] picture;
  | ...
  | 
  | 	public String addPicture()
  | 	{
  | 	    log.info("addPicture() called");
  |     	    log.info("addPicture() - picture size in bytes:#0", picture==null?"0":picture.length);
  |     	    log.info("addPicture() - original filename:#0", pictureOriginalFilename);
  |     	    log.info("addPicture() - contentType:#0", pictureContentType);
  | 		
  |     	    User userToUpdate = em.find(User.class, loggedInUser.getId());
  | 	    if (picture != null && picture.length > 0)
  | 	    {
  | 	        log.info("addPicture() - about to save MemberImage");
  | 	    	
  | 	        // need to save byte[] picture to a unique filename
  | 	        try
  | 	        {
  | 		OutputStream pictureFileStream = new FileOutputStream(pictureOriginalFilename);
  | 		pictureFileStream.write(picture);
  | 		pictureFileStream.close();
  | 	        } catch (Exception e)
  | 	        {
  | 		log.info("addPicture() - Problem saving picture: #0", e);
  | 	        }
  | 	        MemberImage img = new MemberImage();
  | 	        img.setMember(userToUpdate);
  | 	        img.setContentType(pictureContentType);
  | 	        img.setOriginalFilename(pictureOriginalFilename);
  | 	        img.setFilename(pictureOriginalFilename);
  | 	        em.persist(img);
  | 	        log.info("addPicture() - saved MemberImage:#0", img);
  | 
  | 	        userToUpdate.setPicture(img);
  | 	        em.persist(userToUpdate);
  | 
  | 	        loggedInUser = userToUpdate;
  |             }
  | 
  | 	    return "success";
  | 	}
  | 
  | ...
  | 
  | }
  | 

How do I write the file out to my webroot?  Specifically, I want to write them into /content and be able to use s:graphicImage to display it.  Any code examples would be very much appreciated.

Glenn

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4069769#4069769

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4069769



More information about the jboss-user mailing list