0

I try to upload a file (image) using Struts2. But, my file,file content and filename type have a null value ! I tried searching for this problem but without any result.

that is what I'm trying to do :

jsp :

 <s:form id="registerSubmit" method="post" enctype="multipart/form-data" theme="bootstrap">

<s:textfield name="tel" cssClass="form-control" label="tel :"></s:textfield>

<label for="myFile">Upload your file</label>
<input type="file" name="file" />

<button type="submit" id="submit" > Enregistrer</button>
</s:form>

action :

 public class Gestion extends ActionSupport implements  SessionAware, ModelDriven{

        private Visit c;
        private Service Service;
        private Long tel;
        private File file;
        private String fileContentType;
        private String fileFileName;

            public String addvisit(){

                c = new visit();
                Service = new ServiceImpl();            
                c.setTel(tel);
                System.out.println(fileContentType); //null
                System.out.println(fileFileName); //null
                byte[] bFile = null;
                if(file != null)
                    bFile = new byte[(int) file.length()]; 
                c.setPhoto(bFile); // null

                Service.add(c);



        return "success";

            }
     //setters and getters

    }

struts.xml

<struts>
 <constant name="struts.devMode" value="true" />
   <constant name="struts.multipart.maxSize" value="10000000" /> 

    <package name="login" extends="struts-default" namespace="/">

    <action name="addvisit" class="action.Gestion"
            method="addvisit">
               <interceptor-ref name="fileUpload">
                <param name="maximumSize">2097152</param>
                <param name="allowedTypes">
                    image/png,image/gif,image/jpeg,image/pjpeg
                </param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success" type="json" >
                    <param name="root">map</param>
            </result>
        </action>
</package>
</struts>
Roman C
  • 49,761
  • 33
  • 66
  • 176
lilyana
  • 141
  • 1
  • 4
  • 17

1 Answers1

1

You are not pointing to your Action anywhere in your form.

I also suggest you to use (self closed) Struts Tags when possible:

<s:form action="addvisit" id="registerSubmit" method="post" 
        enctype="multipart/form-data" theme="bootstrap">
    <s:textfield name="tel" cssClass="form-control" label="tel :" />
    <s:file name="file" label="Upload your file" />
    <s:submit id="submit" value="Enregistrer" />
</s:form>

That said, your error is 99% related to ModelDriven. Either remove the ModelDriven interface implementation, or configure the stack to have the FileUpload Interceptor after the ModelDriven one, like in the default stack (you are actually using two times the FileUpload interceptor):

<action name="addvisit" class="action.Gestion" method="addvisit">

    <interceptor-ref name="defaultStack">
        <param name="fileUpload.maximumSize">2097152</param>
        <param name="fileUpload.allowedTypes">
            image/png,image/gif,image/jpeg,image/pjpeg
        </param>            
    </interceptor-ref>

    <result name="success" type="json" >
            <param name="root">map</param>
    </result>
</action>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • that is not what you showed us... and it won't work. Please add the missing ajax call to get a better help, and consider jQuery or similar solutions for the AJAX upload, or using a standard submit. – Andrea Ligios May 21 '14 at 10:22
  • even if i call action from submit button, i still had a null value – lilyana May 21 '14 at 10:27
  • i think the mistake was in interceptor call, because when i used a simple form without model driven and ajax cal, i still have the same problem ! – lilyana May 21 '14 at 10:30
  • Now, i get the values of fileContentType and fileFileName, but when i try to convert file to byte[] like this : byte[] bFile = new byte[(int) getFile().length()]; and then i set it : c.setPhoto(bFile), it put null value into database ! any suggestion plase ! – lilyana May 21 '14 at 13:56
  • Now ? Which was the problem ? You should accept / upvote the anwser if it helped (file and attributes are not null anymore), and open a new question if you have a new question. Btw [look here](http://stackoverflow.com/a/859076/1654265) for converting a file to a byte array... – Andrea Ligios May 21 '14 at 14:03
  • Thank you... but I'm still curious about what the problem was :) A typo ? – Andrea Ligios May 21 '14 at 15:06
  • it was about ajax call !! – lilyana May 21 '14 at 15:13
  • 1
    Ohh... I read your `without model driven and ajax cal` like without both, while you was meaning `without model driven and WITH ajax call`... then everything is clear. You can take a look here btw: https://code.google.com/p/ajax-file-upload-struts2/ I've not tried but it seems to suit your needs – Andrea Ligios May 21 '14 at 15:22