Java 使用Struts 2上传图像

Java 使用Struts 2上传图像,java,jsp,file-upload,struts2,Java,Jsp,File Upload,Struts2,我尝试使用以下JSP文件上载图像: 上传用户图片 Struts2文件上传&;保存示例 图片: 行动类别如下: public class InsertImageBean extends ActionSupport{ private static final long serialVersionUID = 1L; private File Image; private String myFileContentType; private Str

我尝试使用以下JSP文件上载图像:


上传用户图片
Struts2文件上传&;保存示例
图片:
行动类别如下:

public class InsertImageBean extends ActionSupport{
   
    private static final long serialVersionUID = 1L;
   
    private File Image;
    private String myFileContentType;
    private String myFileFileName;
   
    public String execute()
    {
        /* Copy file to a safe location */
     
        InsertImage.save(this);

        return SUCCESS;
    }
    public File getMyFile() {
        return Image;
    }
    public void setMyFile(File myFile) {
        this.Image = myFile;
    }
    public String getMyFileContentType() {
        return myFileContentType;
    }
    public void setMyFileContentType(String myFileContentType) {
        this.myFileContentType = myFileContentType;
    }
    public String getMyFileName() {
        return myFileFileName;
    }
    public void setMyFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }
}
public class InsertImage {

    public static int save(InsertImageBean iib) {
        try{
            String filepath = "D:/TEAMWORK/NaTelangana/WebContent/Images";
            File image = iib.getMyFile();
            String myfilename = iib.getMyFileName();
            System.out.println("Src File name: " + iib.getMyFileName());
            System.out.println("Dst File name: " + myfilename);
                     
            File destFile  = new File(filepath, myfilename); // Null pointer exception is thrown here
            FileUtils.copyFile(image, destFile);
            
            System.out.println(System.getProperty("user.dir") );
            
            if(image.renameTo(new File(filepath)))
                System.out.println("File is moved successful!");
            else
                System.out.println("Failed to move!");
        }catch(Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
bean类如下所示:

public class InsertImageBean extends ActionSupport{
   
    private static final long serialVersionUID = 1L;
   
    private File Image;
    private String myFileContentType;
    private String myFileFileName;
   
    public String execute()
    {
        /* Copy file to a safe location */
     
        InsertImage.save(this);

        return SUCCESS;
    }
    public File getMyFile() {
        return Image;
    }
    public void setMyFile(File myFile) {
        this.Image = myFile;
    }
    public String getMyFileContentType() {
        return myFileContentType;
    }
    public void setMyFileContentType(String myFileContentType) {
        this.myFileContentType = myFileContentType;
    }
    public String getMyFileName() {
        return myFileFileName;
    }
    public void setMyFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }
}
public class InsertImage {

    public static int save(InsertImageBean iib) {
        try{
            String filepath = "D:/TEAMWORK/NaTelangana/WebContent/Images";
            File image = iib.getMyFile();
            String myfilename = iib.getMyFileName();
            System.out.println("Src File name: " + iib.getMyFileName());
            System.out.println("Dst File name: " + myfilename);
                     
            File destFile  = new File(filepath, myfilename); // Null pointer exception is thrown here
            FileUtils.copyFile(image, destFile);
            
            System.out.println(System.getProperty("user.dir") );
            
            if(image.renameTo(new File(filepath)))
                System.out.println("File is moved successful!");
            else
                System.out.println("Failed to move!");
        }catch(Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
问题是,在上面的程序中,我在这一行附近的上面的程序中得到了一个
NullPointerException

File destFile  = new File(filepath, myfilename);
问题是bean文件中没有接收到发送的输入。如何纠正此错误

这就是我得到的错误:

java.lang.NullPointerException
位于java.io.File。(File.java:317)
位于natelangana.com.InsertImage.save(InsertImage.java:23)
在bean.com.InsertImageBean.execute(InsertImageBean.java:26)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:606)
在com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
位于com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)上,Src文件名:null
Dst文件名:空
在com.opensymphony.xwork2.intercept.DefaultWorkflowIntercept.doIntercept(DefaultWorkflowIntercept.java:163)
在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
位于org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
在com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept上(ConversionErrorInterceptor.java:122)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
在com.opensymphony.xwork2.interceptor.parameters-sinterceptor.doIntercept(parameters-sinterceptor.java:195)
在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
在com.opensymphony.xwork2.interceptor.parameters-sinterceptor.doIntercept(parameters-sinterceptor.java:195)
在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
在com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept上(StaticParametersInterceptor.java:148)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
在com.opensymphony.xwork2.interceptor.modeldriventerceptor.intercept(modeldriventerceptor.java:89)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于org.apache.struts2.interceptor.ProfilingActivationInterceptor.interceptor(ProfilingActivationInterceptor.java:104)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于org.apache.struts2.interceptor.debug.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于com.opensymphony.xwork2.interceptor.ChainingInterceptor.interceptor(ChainingInterceptor.java:126)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
在com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept上(AliasInterceptor.java:128)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
在com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
位于com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
位于org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation。