Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: uploading files to server using Spring Webflow

  1. #1
    Join Date
    May 2008
    Location
    Colombo, Sri Lanka
    Posts
    19

    Default uploading files to server using Spring Webflow

    Hi all,

    I'm new to Spring webflow and try to develope a file upload application using spring webflow. I went through the file upload example in reference manual. but there is no file upload code. So anyone who know the answer plzzz give me some help to do the work.

    Thank you
    Best Regards,
    Nadeera

  2. #2
    Join Date
    Nov 2005
    Location
    Iasi, Romania
    Posts
    104

    Thumbs up

    Your jsp uploadFile.jsp must have a proper enctype:

    Code:
    <form:form commandName="uploadFileModel" enctype="multipart/form-data">
    Upload file: <input type="file" name="file"/>					
    </form:form>

    and a flow sample:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
    
    	<var name="uploadFileModel" class="yourpackage.UploadFileModel"/>
    	
    	<view-state id="uploadFile" model="uploadFileModel">
    		<transition on="ok" to="finish" >
                       <evaluate expression="yourService.doSomethingWithTheFileContent(uploadFileModel.fileContent)"/>
    
    		<transition on="cancel" to="finish" bind="false"/>
    	</view-state>
    
    	<end-state id="finish"/>
    </flow>
    If you are using swf2.x there are some problems with the binding from MultipartFile to byte[]. So as a temporary solution you can use a model like:

    Code:
    class UploadFileModel implements Serializable {
      private byte[] fileContent;
      
      public byte[] getFileContent() {
        return fileContent;
      }
      
      public void setFile(MultipartFile file) {
        this.fileContent = file.getBytes();
      }
    }
    When the bug http://jira.springframework.org/browse/SWF-696 will be resolved probably you'll have a direct binding to byte[] and the MultipartFile will disappear.

    Hope that this will help.

  3. #3
    Join Date
    May 2008
    Location
    Colombo, Sri Lanka
    Posts
    19

    Default

    Thanx for the help.....

    But I have one question. when we give the model as instanse of UploadFileModel, then when we give the expression is it automatically calls the setters and getters of the UploadFileModel???

    I'm not clear about it... So could help me out plz......

  4. #4
    Join Date
    Nov 2005
    Location
    Iasi, Romania
    Posts
    104

    Default

    When you submit the form spring will create a request parameter of type MultipartFile. This parameter will have the content and some properties of the file that you uploaded. After doing that the request is passed to the swf. Swf will bind the request parameters to the fields of the object that you specified as a model based on the parameter name and type. In your case it will automatically call the model.setFile(MultipartFile file) method because the parameter name is "file".

    And one more thing. Spring needs a MultipartResolver for properly creating the mulitpart parameters. You have to define a bean in the spring servlet context like:

    Code:
    	<bean id ="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    It will be picked up by Spring and used in case of multipart enctype.

    Hope that I was clear enough.

  5. #5
    Join Date
    Jun 2009
    Posts
    1

    Default Spring Multipart File Upload file name with foreign characters

    I am running into a problem with an application that I am working with. We are trying to upload a file name from the windows environment on to a linux server using Spring's Multipart file upload. We have no problems when the files are regular english files; however, if the file has any foreign characters (i.e. french or spanish accents) then Spring complains that it can not find the file. Does anyone have a solution for this?

    Shannon

  6. #6
    Join Date
    Jun 2009
    Posts
    13

    Default Uploaded filne name

    Hi ! this post was really useful indeed!! but I have a question,
    How do I get the file name of the uploaded file?? because I want to store it with it's original file name, but how do I get it??

    Even if this post has been made one year ago I hope that you will read it!

    Many thanks

  7. #7
    Join Date
    Jun 2009
    Posts
    13

    Default

    Quote Originally Posted by PsykoWeb View Post
    Hi ! this post was really useful indeed!! but I have a question,
    How do I get the file name of the uploaded file?? because I want to store it with it's original file name, but how do I get it??

    Even if this post has been made one year ago I hope that you will read it!

    Many thanks

    no ok.. if you save the whole multipart object than it's easy, you just do file.getOriginalFilename()... But I was thinking if the bug will be solved and you can store directly the byte[] how you can do it..

  8. #8
    Join Date
    Jan 2011
    Posts
    1

    Smile Thanks a lot, fanfy!

    Hi, Fanfy,
    Thanks a lot for your post. We are using SWF 2.0. and your post helped me resolve that mystery about multipartFile.
    Hope Spring will fix it soon.
    Thanks again

  9. #9
    Join Date
    Dec 2011
    Location
    Barbuda
    Posts
    1

    Default

    This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your posts on forum.springsource.org . Thanks!

  10. #10
    Join Date
    Jan 2010
    Location
    Ottawa, Canada
    Posts
    37

    Default

    I used richface fileUpload to accomplish the task.

    HTML Code:
    <rich:fileUpload fileUploadListener="#{imageFileUploadBean.listener}"
    							rendered="#{imageFileUploadBean.available }" listHeight="60px" listWidth="auto"
    							maxFilesQuantity="#{imageFileUploadBean.uploadsAvailable}" id="upload"
    							immediateUpload="#{imageFileUploadBean.autoUpload}" acceptedTypes="jpg, gif, png, bmp"
    							cleanButtonClass="clearFileButton" clearAllControlLabel="" clearControlLabel=""
    						>
    							<a4j:support event="onuploadcomplete" reRender="info, thumbnails" />
    						</rich:fileUpload>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •