You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Raghuveer Rawat <ra...@gmail.com> on 2007/10/15 23:53:17 UTC

File Upload using Struts2

Hi, I am trying to upload a jpg image using Struts2 but no luck till now. It
will be great if someone could help me in moving further.

I am using Struts 2.0.8 and i have commons-fileupload-1.2.jar and
commons-io-1.3.2.jar in classpath

My code looks like below.

MyPhoto.jsp

<s:form method="post" action="myPhotoUpload" enctype="multipart/form-data">
<s:file name="myPhoto" cssClass="textbox1" accept="image/*" />
<s:submit name="method:upload" cssClass="btn" value="Upload" />
</s:form>

Action class: MyPhotoUploadAction.java

private File myPhoto;//The actual file
    private String myPhotoContentType; //The content type of the file
    private String myPhotoFileName; //The uploaded file name

 public File getMyPhoto() {
        return myPhoto;
      }

      public void setMyPhoto(File myPhoto) {
        System.out.println("Inside setMyPhoto");
        this.myPhoto = myPhoto;
      }

      public String getMyPhotoContentType() {
        return myPhotoContentType;
      }

      public void setMyPhotoContentType(String myPhotoContentType) {
        System.out.println("Inside setMyPhotoContentType");
        this.myPhotoContentType = myPhotoContentType;
      }

      public String getMyPhotoFileName() {
          System.out.println("Inside getMyPhotoFileName:"+myPhotoFileName);
        return myPhotoFileName;
      }

      public void setMyPhotoFileName(String myPhotoFileName) {
        System.out.println("Inside setMyPhotoFileName:"+myPhotoFileName);
        this.myPhotoFileName = myPhotoFileName;
      }

      public String upload() {
         try{
            System.out.println("Inside Upload");
            File destDir = new File("images/member");
             System.out.println("File Name:"+this.getMyPhotoFileName());
             System.out.println("File
ContentType:"+this.getMyPhotoContentType());
             FileUtils.copyFileToDirectory(getMyPhoto(), destDir);
             return SUCCESS; // redirect to welcome page.
         }catch(Exception exp){
             LOG.error("File Upload Error:"+ exp);
             this.addActionError("File Upload Error:"+ exp);
             return ERROR;
         }
      }

Fwd: File Upload using Struts2

Posted by Raghuveer Rawat <ra...@gmail.com>.
Hi, I am new to sturts2. I am trying to upload a jpg image using Struts2 but
no luck till now. It will be great if someone could help me in moving
further.

I am using Struts 2.0.8 and i have commons-fileupload-1.2.jar and
commons-io-1.3.2.jar in classpath

I put debug statement in setter methods. I can see even setter methods are
not getting invoked. File, FileName, ContentType are all null at Action
class.

My code looks like below.

MyPhoto.jsp

<s:form method="post" action="myPhotoUpload" enctype="multipart/form-data">
<s:file name="myPhoto" cssClass="textbox1" accept="image/*" />
<s:submit name="method:upload" cssClass="btn" value="Upload" />
</s:form>

Action class: MyPhotoUploadAction.java

    private File myPhoto;//The actual file
    private String myPhotoContentType; //The content type of the file
    private String myPhotoFileName; //The uploaded file name

     public File getMyPhoto() {
        return myPhoto;
      }

      public void setMyPhoto(File myPhoto) {
         this.myPhoto = myPhoto;
      }

      public String getMyPhotoContentType() {
        return myPhotoContentType;
      }

      public void setMyPhotoContentType(String myPhotoContentType) {
         this.myPhotoContentType = myPhotoContentType;
      }

      public String getMyPhotoFileName() {
        return myPhotoFileName;
      }

      public void setMyPhotoFileName(String myPhotoFileName) {
           this.myPhotoFileName = myPhotoFileName;
      }

      public String upload() {
         try{
             File destDir = new File("images/member");
             FileUtils.copyFileToDirectory (getMyPhoto(), destDir);
             return SUCCESS; // redirect to welcome page.
         }catch(Exception exp){
             LOG.error("File Upload Error:"+ exp);
             this.addActionError("File Upload Error:"+ exp);
             return ERROR;
         }
      }


Struts.xml
<struts>
    <package name="Non-RPC" namespace="/" extends="tiles-default">
<result-types>

            <result-type name="tiles" class="
org.apache.struts2.views.tiles.TilesResult" default="true" />
        </result-types>

<action name="myPhotoUpload_*" method="{1}" class="
com.rawatsoft.write4smile.webapp.action.MyPhotoUploadAction">
            <interceptor-ref name="fileUpload">
                  <param name="allowedTypes">
                    image/*
                </param>
            </interceptor-ref>
            <interceptor-ref name="basicStack"/>
            <result name="input">myPhotoUpload</result>
        </action>
 </package>

web.xml

    <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>

    </filter>

    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
    </filter>

    <filter>
        <filter-name>filterDispatcher</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>filterDispatcher</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
    </listener>

RE: File Upload using Struts2

Posted by Dave Newton <ne...@yahoo.com>.
--- Rajagopal_Yendluri <Ra...@satyam.com>
wrote:
> How can I get the actual file name which I browsed?

http://struts.apache.org/2.x/docs/file-upload-interceptor.html

By providing a method String fileName property in your
action. Note that different browsers may send the
actual filename differently (like not sending the
entire path, just the filename).

d.

> 
> -----Original Message-----
> From: Raghuveer Rawat
> [mailto:raghuveer.rawat@gmail.com] 
> Sent: Tuesday, October 16, 2007 10:31 AM
> To: Struts Users Mailing List
> Subject: Re: File Upload using Struts2
> 
> Thanks Dave for reply..
> I have no clue what is happening...I can see below
> statement in log..
> 
> Content-Type not allowed: myPhoto
> "upload_460c03b6_115a6ef087d__8000_00000000.tmp"
> image/jpeg
> 
> I have configured filters like below in web.xml.. I
> don't have context
> clean-up filter. Where should I place this filter?
> 
> <filter>
>        
> <filter-name>openSessionInViewFilter</filter-name>
>         <filter-class>
>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
> </filter-class>
>     </filter>
> 
>     <filter>
>         <filter-name>filterDispatcher</filter-name>
>        
>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher
> </filter-class>
>     </filter>
> 
>     <filter-mapping>
>        
> <filter-name>openSessionInViewFilter</filter-name>
>         <url-pattern>/*</url-pattern>
>     </filter-mapping>
> 
>     <filter-mapping>
>         <filter-name>filterDispatcher</filter-name>
>         <url-pattern>/*</url-pattern>
>     </filter-mapping>
> 
>     <listener>
>         <listener-class>
>
org.springframework.web.context.ContextLoaderListener</listener-class>
>     </listener>
> 
>     <listener>
>        
>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener
> </listener-class>
>     </listener>
> 
> Thanks
> Raghu
> 
> 
> 
> On 10/15/07, Dave Newton <ne...@yahoo.com>
> wrote:
> >
> > In general it's a good idea to include things like
> > what is actually happening: what symptoms (besides
> > "the file isn't uploading" :) are there? Is there
> > anything in the logs?
> >
> > Are you including the context cleanup filter in
> your
> > web.xml?
> >
> > d.
> >
> > --- Raghuveer Rawat <ra...@gmail.com>
> wrote:
> >
> > > Hi, I am trying to upload a jpg image using
> Struts2
> > > but no luck till now. It
> > > will be great if someone could help me in moving
> > > further.
> > >
> > > I am using Struts 2.0.8 and i have
> > > commons-fileupload-1.2.jar and
> > > commons-io-1.3.2.jar in classpath
> > >
> > > My code looks like below.
> > >
> > > MyPhoto.jsp
> > >
> > > <s:form method="post" action="myPhotoUpload"
> > > enctype="multipart/form-data">
> > > <s:file name="myPhoto" cssClass="textbox1"
> > > accept="image/*" />
> > > <s:submit name="method:upload" cssClass="btn"
> > > value="Upload" />
> > > </s:form>
> > >
> > > Action class: MyPhotoUploadAction.java
> > >
> > > private File myPhoto;//The actual file
> > >     private String myPhotoContentType; //The
> content
> > > type of the file
> > >     private String myPhotoFileName; //The
> uploaded
> > > file name
> > >
> > >  public File getMyPhoto() {
> > >         return myPhoto;
> > >       }
> > >
> > >       public void setMyPhoto(File myPhoto) {
> > >         System.out.println("Inside setMyPhoto");
> > >         this.myPhoto = myPhoto;
> > >       }
> > >
> > >       public String getMyPhotoContentType() {
> > >         return myPhotoContentType;
> > >       }
> > >
> > >       public void setMyPhotoContentType(String
> > > myPhotoContentType) {
> > >         System.out.println("Inside
> > > setMyPhotoContentType");
> > >         this.myPhotoContentType =
> > > myPhotoContentType;
> > >       }
> > >
> > >       public String getMyPhotoFileName() {
> > >           System.out.println("Inside
> > > getMyPhotoFileName:"+myPhotoFileName);
> > >         return myPhotoFileName;
> > >       }
> > >
> > >       public void setMyPhotoFileName(String
> > > myPhotoFileName) {
> > >         System.out.println("Inside
> > > setMyPhotoFileName:"+myPhotoFileName);
> > >         this.myPhotoFileName = myPhotoFileName;
> > >       }
> > >
> > >       public String upload() {
> > >          try{
> > >             System.out.println("Inside Upload");
> > >             File destDir = new
> > > File("images/member");
> > >              System.out.println("File
> > > Name:"+this.getMyPhotoFileName());
> > >              System.out.println("File
> > > ContentType:"+this.getMyPhotoContentType());
> > >
> > > FileUtils.copyFileToDirectory(getMyPhoto(),
> > > destDir);
> > >              return SUCCESS; // redirect to
> welcome
> > > page.
> > >          }catch(Exception exp){
> > >              LOG.error("File Upload Error:"+
> exp);
> > >              this.addActionError("File Upload
> > > Error:"+ exp);
> > >              return ERROR;
> > >          }
> > >       }
> > >
> >
> >
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> > For additional commands, e-mail:
> user-help@struts.apache.org
> >
> >
> 
> 
> DISCLAIMER:
> This email (including any attachments) is intended
> for the sole use of the intended recipient/s and may
> contain material that is CONFIDENTIAL AND PRIVATE
> COMPANY INFORMATION. Any review or reliance by
> others or copying or distribution or forwarding of
> any or all of the contents in this message is
> STRICTLY PROHIBITED. If you are not the intended
> recipient, please contact the sender by email and
> delete all copies; your cooperation in this regard
> is appreciated.
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 
=== message truncated ===


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


RE: File Upload using Struts2

Posted by Rajagopal_Yendluri <Ra...@satyam.com>.
How can I get the actual file name which I browsed?

-----Original Message-----
From: Raghuveer Rawat [mailto:raghuveer.rawat@gmail.com] 
Sent: Tuesday, October 16, 2007 10:31 AM
To: Struts Users Mailing List
Subject: Re: File Upload using Struts2

Thanks Dave for reply..
I have no clue what is happening...I can see below statement in log..

Content-Type not allowed: myPhoto
"upload_460c03b6_115a6ef087d__8000_00000000.tmp" image/jpeg

I have configured filters like below in web.xml.. I don't have context
clean-up filter. Where should I place this filter?

<filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
    </filter>

    <filter>
        <filter-name>filterDispatcher</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>filterDispatcher</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
    </listener>

Thanks
Raghu



On 10/15/07, Dave Newton <ne...@yahoo.com> wrote:
>
> In general it's a good idea to include things like
> what is actually happening: what symptoms (besides
> "the file isn't uploading" :) are there? Is there
> anything in the logs?
>
> Are you including the context cleanup filter in your
> web.xml?
>
> d.
>
> --- Raghuveer Rawat <ra...@gmail.com> wrote:
>
> > Hi, I am trying to upload a jpg image using Struts2
> > but no luck till now. It
> > will be great if someone could help me in moving
> > further.
> >
> > I am using Struts 2.0.8 and i have
> > commons-fileupload-1.2.jar and
> > commons-io-1.3.2.jar in classpath
> >
> > My code looks like below.
> >
> > MyPhoto.jsp
> >
> > <s:form method="post" action="myPhotoUpload"
> > enctype="multipart/form-data">
> > <s:file name="myPhoto" cssClass="textbox1"
> > accept="image/*" />
> > <s:submit name="method:upload" cssClass="btn"
> > value="Upload" />
> > </s:form>
> >
> > Action class: MyPhotoUploadAction.java
> >
> > private File myPhoto;//The actual file
> >     private String myPhotoContentType; //The content
> > type of the file
> >     private String myPhotoFileName; //The uploaded
> > file name
> >
> >  public File getMyPhoto() {
> >         return myPhoto;
> >       }
> >
> >       public void setMyPhoto(File myPhoto) {
> >         System.out.println("Inside setMyPhoto");
> >         this.myPhoto = myPhoto;
> >       }
> >
> >       public String getMyPhotoContentType() {
> >         return myPhotoContentType;
> >       }
> >
> >       public void setMyPhotoContentType(String
> > myPhotoContentType) {
> >         System.out.println("Inside
> > setMyPhotoContentType");
> >         this.myPhotoContentType =
> > myPhotoContentType;
> >       }
> >
> >       public String getMyPhotoFileName() {
> >           System.out.println("Inside
> > getMyPhotoFileName:"+myPhotoFileName);
> >         return myPhotoFileName;
> >       }
> >
> >       public void setMyPhotoFileName(String
> > myPhotoFileName) {
> >         System.out.println("Inside
> > setMyPhotoFileName:"+myPhotoFileName);
> >         this.myPhotoFileName = myPhotoFileName;
> >       }
> >
> >       public String upload() {
> >          try{
> >             System.out.println("Inside Upload");
> >             File destDir = new
> > File("images/member");
> >              System.out.println("File
> > Name:"+this.getMyPhotoFileName());
> >              System.out.println("File
> > ContentType:"+this.getMyPhotoContentType());
> >
> > FileUtils.copyFileToDirectory(getMyPhoto(),
> > destDir);
> >              return SUCCESS; // redirect to welcome
> > page.
> >          }catch(Exception exp){
> >              LOG.error("File Upload Error:"+ exp);
> >              this.addActionError("File Upload
> > Error:"+ exp);
> >              return ERROR;
> >          }
> >       }
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


DISCLAIMER:
This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated.

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


Re: File Upload using Struts2

Posted by Raghuveer Rawat <ra...@gmail.com>.
I already have enctype="multipart/form-data"

I can see below errors in log.. Looks like request is getting terminated
befor

Content-Type not allowed: myPhoto
"upload_7ddb4d8f_115a947dc12__8000_00000000.tmp" image/pjpeg
java.lang.NullPointerException: Source must not be null

Looks like request is getting complete before it reaches to FileUpload.
How to make this request available to FileUpload?

Thanks
Raghu

On 10/16/07, Rajagopal_Yendluri <Ra...@satyam.com> wrote:
>
> Hi Raghu,
>
>
>
>    There should be no problem with the configuration, you have to set
> the "enctype"
>
>
>
> Here is my code:
>
>
>
> JSP
>
>
>
>
>
> <s:form action="Login" enctype="multipart/form-data">
>
>
>
> <s:file name="myFile" label="UploadFile"/>
>
>
>
> Action
>
>
>
>            File f = new File("C:\\Tomcat 6.0\\abcd.jpeg");
>
>            FileUtils.copyFile(getMyFile(), f);
>
>
>
>
>
> Find out where is the problem in your code, if not able to please post
> the code.
>
>
>
> Regards,
>
> Rajagopal Y
>
> HCU-Consulting & Enterprise Solutions.
>
> Phone: (C) +91-9886876114 / (W) 6658 3685.
>
>
>
>
>
> -----Original Message-----
> From: Raghuveer Rawat [mailto:raghuveer.rawat@gmail.com]
> Sent: Tuesday, October 16, 2007 10:31 AM
> To: Struts Users Mailing List
> Subject: Re: File Upload using Struts2
>
>
>
> Thanks Dave for reply..
>
> I have no clue what is happening...I can see below statement in log..
>
>
>
> Content-Type not allowed: myPhoto
>
> "upload_460c03b6_115a6ef087d__8000_00000000.tmp" image/jpeg
>
>
>
> I have configured filters like below in web.xml.. I don't have context
>
> clean-up filter. Where should I place this filter?
>
>
>
> <filter>
>
>        <filter-name>openSessionInViewFilter</filter-name>
>
>        <filter-class>
>
> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
>
> </filter-class>
>
>    </filter>
>
>
>
>    <filter>
>
>        <filter-name>filterDispatcher</filter-name>
>
>        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
>
> </filter-class>
>
>    </filter>
>
>
>
>    <filter-mapping>
>
>        <filter-name>openSessionInViewFilter</filter-name>
>
>        <url-pattern>/*</url-pattern>
>
>    </filter-mapping>
>
>
>
>    <filter-mapping>
>
>        <filter-name>filterDispatcher</filter-name>
>
>        <url-pattern>/*</url-pattern>
>
>    </filter-mapping>
>
>
>
>    <listener>
>
>        <listener-class>
>
> org.springframework.web.context.ContextLoaderListener</listener-class>
>
>    </listener>
>
>
>
>    <listener>
>
>        <listener-class>org.apache.struts2.tiles.StrutsTilesListener
>
> </listener-class>
>
>    </listener>
>
>
>
> Thanks
>
> Raghu
>
>
>
>
>
>
>
> On 10/15/07, Dave Newton <ne...@yahoo.com> wrote:
>
> >
>
> > In general it's a good idea to include things like
>
> > what is actually happening: what symptoms (besides
>
> > "the file isn't uploading" :) are there? Is there
>
> > anything in the logs?
>
> >
>
> > Are you including the context cleanup filter in your
>
> > web.xml?
>
> >
>
> > d.
>
> >
>
> > --- Raghuveer Rawat <ra...@gmail.com> wrote:
>
> >
>
> > > Hi, I am trying to upload a jpg image using Struts2
>
> > > but no luck till now. It
>
> > > will be great if someone could help me in moving
>
> > > further.
>
> > >
>
> > > I am using Struts 2.0.8 and i have
>
> > > commons-fileupload-1.2.jar and
>
> > > commons-io-1.3.2.jar in classpath
>
> > >
>
> > > My code looks like below.
>
> > >
>
> > > MyPhoto.jsp
>
> > >
>
> > > <s:form method="post" action="myPhotoUpload"
>
> > > enctype="multipart/form-data">
>
> > > <s:file name="myPhoto" cssClass="textbox1"
>
> > > accept="image/*" />
>
> > > <s:submit name="method:upload" cssClass="btn"
>
> > > value="Upload" />
>
> > > </s:form>
>
> > >
>
> > > Action class: MyPhotoUploadAction.java
>
> > >
>
> > > private File myPhoto;//The actual file
>
> > >     private String myPhotoContentType; //The content
>
> > > type of the file
>
> > >     private String myPhotoFileName; //The uploaded
>
> > > file name
>
> > >
>
> > >  public File getMyPhoto() {
>
> > >         return myPhoto;
>
> > >       }
>
> > >
>
> > >       public void setMyPhoto(File myPhoto) {
>
> > >         System.out.println("Inside setMyPhoto");
>
> > >         this.myPhoto = myPhoto;
>
> > >       }
>
> > >
>
> > >       public String getMyPhotoContentType() {
>
> > >         return myPhotoContentType;
>
> > >       }
>
> > >
>
> > >       public void setMyPhotoContentType(String
>
> > > myPhotoContentType) {
>
> > >         System.out.println("Inside
>
> > > setMyPhotoContentType");
>
> > >         this.myPhotoContentType =
>
> > > myPhotoContentType;
>
> > >       }
>
> > >
>
> > >       public String getMyPhotoFileName() {
>
> > >           System.out.println("Inside
>
> > > getMyPhotoFileName:"+myPhotoFileName);
>
> > >         return myPhotoFileName;
>
> > >       }
>
> > >
>
> > >       public void setMyPhotoFileName(String
>
> > > myPhotoFileName) {
>
> > >         System.out.println("Inside
>
> > > setMyPhotoFileName:"+myPhotoFileName);
>
> > >         this.myPhotoFileName = myPhotoFileName;
>
> > >       }
>
> > >
>
> > >       public String upload() {
>
> > >          try{
>
> > >             System.out.println("Inside Upload");
>
> > >             File destDir = new
>
> > > File("images/member");
>
> > >              System.out.println("File
>
> > > Name:"+this.getMyPhotoFileName());
>
> > >              System.out.println("File
>
> > > ContentType:"+this.getMyPhotoContentType());
>
> > >
>
> > > FileUtils.copyFileToDirectory(getMyPhoto(),
>
> > > destDir);
>
> > >              return SUCCESS; // redirect to welcome
>
> > > page.
>
> > >          }catch(Exception exp){
>
> > >              LOG.error("File Upload Error:"+ exp);
>
> > >              this.addActionError("File Upload
>
> > > Error:"+ exp);
>
> > >              return ERROR;
>
> > >          }
>
> > >       }
>
> > >
>
> >
>
> >
>
> > ---------------------------------------------------------------------
>
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>
> > For additional commands, e-mail: user-help@struts.apache.org
>
> >
>
> >
>
>
>
> DISCLAIMER:
> This email (including any attachments) is intended for the sole use of the
> intended recipient/s and may contain material that is CONFIDENTIAL AND
> PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or
> distribution or forwarding of any or all of the contents in this message is
> STRICTLY PROHIBITED. If you are not the intended recipient, please contact
> the sender by email and delete all copies; your cooperation in this regard
> is appreciated.
>

RE: File Upload using Struts2

Posted by Rajagopal_Yendluri <Ra...@satyam.com>.
Hi Raghu,

 

    There should be no problem with the configuration, you have to set
the "enctype"

 

Here is my code: 

 

JSP

 

      

<s:form action="Login" enctype="multipart/form-data">

 

<s:file name="myFile" label="UploadFile"/>

 

Action

 

            File f = new File("C:\\Tomcat 6.0\\abcd.jpeg");

            FileUtils.copyFile(getMyFile(), f);

 

 

Find out where is the problem in your code, if not able to please post
the code.

 

Regards,

Rajagopal Y

HCU-Consulting & Enterprise Solutions.

Phone: (C) +91-9886876114 / (W) 6658 3685.

 

 

-----Original Message-----
From: Raghuveer Rawat [mailto:raghuveer.rawat@gmail.com] 
Sent: Tuesday, October 16, 2007 10:31 AM
To: Struts Users Mailing List
Subject: Re: File Upload using Struts2

 

Thanks Dave for reply..

I have no clue what is happening...I can see below statement in log..

 

Content-Type not allowed: myPhoto

"upload_460c03b6_115a6ef087d__8000_00000000.tmp" image/jpeg

 

I have configured filters like below in web.xml.. I don't have context

clean-up filter. Where should I place this filter?

 

<filter>

        <filter-name>openSessionInViewFilter</filter-name>

        <filter-class>

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

</filter-class>

    </filter>

 

    <filter>

        <filter-name>filterDispatcher</filter-name>

        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher

</filter-class>

    </filter>

 

    <filter-mapping>

        <filter-name>openSessionInViewFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

 

    <filter-mapping>

        <filter-name>filterDispatcher</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

 

    <listener>

        <listener-class>

org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

 

    <listener>

        <listener-class>org.apache.struts2.tiles.StrutsTilesListener

</listener-class>

    </listener>

 

Thanks

Raghu

 

 

 

On 10/15/07, Dave Newton <ne...@yahoo.com> wrote:

> 

> In general it's a good idea to include things like

> what is actually happening: what symptoms (besides

> "the file isn't uploading" :) are there? Is there

> anything in the logs?

> 

> Are you including the context cleanup filter in your

> web.xml?

> 

> d.

> 

> --- Raghuveer Rawat <ra...@gmail.com> wrote:

> 

> > Hi, I am trying to upload a jpg image using Struts2

> > but no luck till now. It

> > will be great if someone could help me in moving

> > further.

> >

> > I am using Struts 2.0.8 and i have

> > commons-fileupload-1.2.jar and

> > commons-io-1.3.2.jar in classpath

> >

> > My code looks like below.

> >

> > MyPhoto.jsp

> >

> > <s:form method="post" action="myPhotoUpload"

> > enctype="multipart/form-data">

> > <s:file name="myPhoto" cssClass="textbox1"

> > accept="image/*" />

> > <s:submit name="method:upload" cssClass="btn"

> > value="Upload" />

> > </s:form>

> >

> > Action class: MyPhotoUploadAction.java

> >

> > private File myPhoto;//The actual file

> >     private String myPhotoContentType; //The content

> > type of the file

> >     private String myPhotoFileName; //The uploaded

> > file name

> >

> >  public File getMyPhoto() {

> >         return myPhoto;

> >       }

> >

> >       public void setMyPhoto(File myPhoto) {

> >         System.out.println("Inside setMyPhoto");

> >         this.myPhoto = myPhoto;

> >       }

> >

> >       public String getMyPhotoContentType() {

> >         return myPhotoContentType;

> >       }

> >

> >       public void setMyPhotoContentType(String

> > myPhotoContentType) {

> >         System.out.println("Inside

> > setMyPhotoContentType");

> >         this.myPhotoContentType =

> > myPhotoContentType;

> >       }

> >

> >       public String getMyPhotoFileName() {

> >           System.out.println("Inside

> > getMyPhotoFileName:"+myPhotoFileName);

> >         return myPhotoFileName;

> >       }

> >

> >       public void setMyPhotoFileName(String

> > myPhotoFileName) {

> >         System.out.println("Inside

> > setMyPhotoFileName:"+myPhotoFileName);

> >         this.myPhotoFileName = myPhotoFileName;

> >       }

> >

> >       public String upload() {

> >          try{

> >             System.out.println("Inside Upload");

> >             File destDir = new

> > File("images/member");

> >              System.out.println("File

> > Name:"+this.getMyPhotoFileName());

> >              System.out.println("File

> > ContentType:"+this.getMyPhotoContentType());

> >

> > FileUtils.copyFileToDirectory(getMyPhoto(),

> > destDir);

> >              return SUCCESS; // redirect to welcome

> > page.

> >          }catch(Exception exp){

> >              LOG.error("File Upload Error:"+ exp);

> >              this.addActionError("File Upload

> > Error:"+ exp);

> >              return ERROR;

> >          }

> >       }

> >

> 

> 

> ---------------------------------------------------------------------

> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org

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

> 

> 



DISCLAIMER:
This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated.

Re: File Upload using Struts2

Posted by Raghuveer Rawat <ra...@gmail.com>.
Thanks Dave for reply..
I have no clue what is happening...I can see below statement in log..

Content-Type not allowed: myPhoto
"upload_460c03b6_115a6ef087d__8000_00000000.tmp" image/jpeg

I have configured filters like below in web.xml.. I don't have context
clean-up filter. Where should I place this filter?

<filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
    </filter>

    <filter>
        <filter-name>filterDispatcher</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>filterDispatcher</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
    </listener>

Thanks
Raghu



On 10/15/07, Dave Newton <ne...@yahoo.com> wrote:
>
> In general it's a good idea to include things like
> what is actually happening: what symptoms (besides
> "the file isn't uploading" :) are there? Is there
> anything in the logs?
>
> Are you including the context cleanup filter in your
> web.xml?
>
> d.
>
> --- Raghuveer Rawat <ra...@gmail.com> wrote:
>
> > Hi, I am trying to upload a jpg image using Struts2
> > but no luck till now. It
> > will be great if someone could help me in moving
> > further.
> >
> > I am using Struts 2.0.8 and i have
> > commons-fileupload-1.2.jar and
> > commons-io-1.3.2.jar in classpath
> >
> > My code looks like below.
> >
> > MyPhoto.jsp
> >
> > <s:form method="post" action="myPhotoUpload"
> > enctype="multipart/form-data">
> > <s:file name="myPhoto" cssClass="textbox1"
> > accept="image/*" />
> > <s:submit name="method:upload" cssClass="btn"
> > value="Upload" />
> > </s:form>
> >
> > Action class: MyPhotoUploadAction.java
> >
> > private File myPhoto;//The actual file
> >     private String myPhotoContentType; //The content
> > type of the file
> >     private String myPhotoFileName; //The uploaded
> > file name
> >
> >  public File getMyPhoto() {
> >         return myPhoto;
> >       }
> >
> >       public void setMyPhoto(File myPhoto) {
> >         System.out.println("Inside setMyPhoto");
> >         this.myPhoto = myPhoto;
> >       }
> >
> >       public String getMyPhotoContentType() {
> >         return myPhotoContentType;
> >       }
> >
> >       public void setMyPhotoContentType(String
> > myPhotoContentType) {
> >         System.out.println("Inside
> > setMyPhotoContentType");
> >         this.myPhotoContentType =
> > myPhotoContentType;
> >       }
> >
> >       public String getMyPhotoFileName() {
> >           System.out.println("Inside
> > getMyPhotoFileName:"+myPhotoFileName);
> >         return myPhotoFileName;
> >       }
> >
> >       public void setMyPhotoFileName(String
> > myPhotoFileName) {
> >         System.out.println("Inside
> > setMyPhotoFileName:"+myPhotoFileName);
> >         this.myPhotoFileName = myPhotoFileName;
> >       }
> >
> >       public String upload() {
> >          try{
> >             System.out.println("Inside Upload");
> >             File destDir = new
> > File("images/member");
> >              System.out.println("File
> > Name:"+this.getMyPhotoFileName());
> >              System.out.println("File
> > ContentType:"+this.getMyPhotoContentType());
> >
> > FileUtils.copyFileToDirectory(getMyPhoto(),
> > destDir);
> >              return SUCCESS; // redirect to welcome
> > page.
> >          }catch(Exception exp){
> >              LOG.error("File Upload Error:"+ exp);
> >              this.addActionError("File Upload
> > Error:"+ exp);
> >              return ERROR;
> >          }
> >       }
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: File Upload using Struts2

Posted by Dave Newton <ne...@yahoo.com>.
In general it's a good idea to include things like
what is actually happening: what symptoms (besides
"the file isn't uploading" :) are there? Is there
anything in the logs?

Are you including the context cleanup filter in your
web.xml?

d.

--- Raghuveer Rawat <ra...@gmail.com> wrote:

> Hi, I am trying to upload a jpg image using Struts2
> but no luck till now. It
> will be great if someone could help me in moving
> further.
> 
> I am using Struts 2.0.8 and i have
> commons-fileupload-1.2.jar and
> commons-io-1.3.2.jar in classpath
> 
> My code looks like below.
> 
> MyPhoto.jsp
> 
> <s:form method="post" action="myPhotoUpload"
> enctype="multipart/form-data">
> <s:file name="myPhoto" cssClass="textbox1"
> accept="image/*" />
> <s:submit name="method:upload" cssClass="btn"
> value="Upload" />
> </s:form>
> 
> Action class: MyPhotoUploadAction.java
> 
> private File myPhoto;//The actual file
>     private String myPhotoContentType; //The content
> type of the file
>     private String myPhotoFileName; //The uploaded
> file name
> 
>  public File getMyPhoto() {
>         return myPhoto;
>       }
> 
>       public void setMyPhoto(File myPhoto) {
>         System.out.println("Inside setMyPhoto");
>         this.myPhoto = myPhoto;
>       }
> 
>       public String getMyPhotoContentType() {
>         return myPhotoContentType;
>       }
> 
>       public void setMyPhotoContentType(String
> myPhotoContentType) {
>         System.out.println("Inside
> setMyPhotoContentType");
>         this.myPhotoContentType =
> myPhotoContentType;
>       }
> 
>       public String getMyPhotoFileName() {
>           System.out.println("Inside
> getMyPhotoFileName:"+myPhotoFileName);
>         return myPhotoFileName;
>       }
> 
>       public void setMyPhotoFileName(String
> myPhotoFileName) {
>         System.out.println("Inside
> setMyPhotoFileName:"+myPhotoFileName);
>         this.myPhotoFileName = myPhotoFileName;
>       }
> 
>       public String upload() {
>          try{
>             System.out.println("Inside Upload");
>             File destDir = new
> File("images/member");
>              System.out.println("File
> Name:"+this.getMyPhotoFileName());
>              System.out.println("File
> ContentType:"+this.getMyPhotoContentType());
>             
> FileUtils.copyFileToDirectory(getMyPhoto(),
> destDir);
>              return SUCCESS; // redirect to welcome
> page.
>          }catch(Exception exp){
>              LOG.error("File Upload Error:"+ exp);
>              this.addActionError("File Upload
> Error:"+ exp);
>              return ERROR;
>          }
>       }
> 


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