Announcement Announcement Module
Collapse
No announcement yet.
uploading files to server using Spring Webflow Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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......

      Comment


      • #4
        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.

        Comment


        • #5
          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

          Comment


          • #6
            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

            Comment


            • #7
              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..

              Comment


              • #8
                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

                Comment


                • #9
                  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!

                  Comment


                  • #10
                    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>

                    Comment


                    • #11
                      Great helpful information, Thanks for this nice post,
                      keep it up.

                      Comment


                      • #12
                        Hi, I'm Carlos from Chile,

                        I create the same sample to test upload file using WebFlow 2, but I' don't have errors, the request on firebug plugin of Mozilla show that there is not problem, but the file donīt arrived to Spring Controller, I have a break point to debug the data in the request but is imposible, donīt arrived data to my controller.
                        Please you have a example with source code that works with out errors.

                        Thanks my friend, and excuse my english.

                        Comment


                        • #13
                          hello!
                          if I wanted to upload more than one file?

                          Comment

                          Working...
                          X