Hello Every one
I m new Struts 2 and i m trying to upload a image on server with the help of struts 2 but i was unable to do that i m pasting my code here ..
please tell where i m wrong

here is my action class

package com.webportal.admin.action;

import java.io.File;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.webportal.admin.model.ImageDetail;

public class SetInfo extends ActionSupport implements ServletRequestAware{
    private ImageDetail imageDetailBean;
    HttpServletRequest servletRequest;
        private static final long serialVersionUID = 1L;

public String execute() throws Exception
        {
        boolean done=upload();
        if(done)
        return "success";
        else
        return "input"
    }
    private boolean upload() { 
            try {
                    String filePath = servletRequest.getSession().getServletContext().getContextPath();
                    System.out.println("\nServer path:" + filePath+"      "+imageDetailBean.getLogoName()+"......."); 
                    File fileToCreate = new File(filePath, imageDetailBean.getLogoName());
                    FileUtils.copyFile(imageDetailBean.getLogo(), fileToCreate);
                } catch (Exception e) {
                    e.printStackTrace();
                    addActionError(e.getMessage());

                    return false;
                }
                return true;

            }

    public ImageDetail getImageDetailBean() {
        return imageDetailBean;
    }

    public void setImageDetailBean(ImageDetail imageDetailBean) {
        this.imageDetailBean = imageDetailBean;

    }   
    public void setServletRequest(HttpServletRequest servletRequest) {
            this.servletRequest = servletRequest;
    }
}

my beans class is

package com.webportal.admin.model;

import java.io.File;

public class ImageDetail {

    private String logoContentType;
    private String logoName;
    private File logo;

    public File getLogo() {
        return logo;
    }   
    public void setLogo(File logo) {
        this.logo = logo;
    }


    public String getLogoContentType() {
        return logoContentType;
    }
    public void setLogoContentType(String logoContentType) {
        this.logoContentType = logoContentType;
    }


    public String getLogoName() {
        return logoName;
    }

    public void setLogoName(String logoName) {
        this.logoName = logoName;
    }

public ImageDetail() {
        super();
        System.out.print("from the constructor Image detail");
    }

}

now my jsp from where i set my bean

    <s:form action="settingHeader" cssClass="form" enctype="multipart/form-data" method="post"  >
           <s:div cssClass="fileinputs">
                <s:file  key="imageDetailBean.logo"  />
           </s:div>
    </s:form>

my property file
imageDetailBean.logo=Logo

struts.xml

<action name="settingHeader" class="com.webportal.admin.action.SetInfo" method="execute" >
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">2097152</param>
                <param name="allowedTypes">
                        image/png,image/gif,image/jpeg,image/jpg
                </param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack">
            </interceptor-ref>
            <result name="input">/success.jsp</result>
            <result name="success">/error.jsp</result>
        </action>

when i deploy my application i got nullpointerexception on line number 30 and on line number 29 it prints null on server
please suggest me what can i do...
thanking you

In line number 29 and 30 the code

System.out.println("\nServer path:"+filePath+" "+imageDetailBean.getLogoName()+"......."); 
File fileToCreate = new File(filePath, imageDetailBean.getLogoName());

Rename the code setLogoName -> setLogoFileName

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.