报错如下,是什么原因啊?怎么解决呢?
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
java.io.FileInputStream.<init>(FileInputStream.java:103)
java.io.FileInputStream.<init>(FileInputStream.java:66)
sunyang.UploadAction.execute(UploadAction.java:56)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.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.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.ja

解决方案 »

  1.   

    空指针。仔细检查下后台action代码
      

  2.   


    action如下package sunyang;import java.io.FileInputStream;
    import java.io.FileOutputStream;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;
    import org.apache.struts2.components.File;import com.opensymphony.xwork2.ActionSupport;
    public class UploadAction extends ActionSupport{
    private String title;//上传文件标题
    private File sunyangLogo;//上会文件
    private String sunyangLogoContentType;//上传文件类型
    private String sunyangLogoFileName;//上传文件名
    private String savePath;//上传文件保存路径

    public String getTitle() {
    return title;
    }
    public void setTitle(String title) {
    this.title = title;
    }

    //上传文件的getter和setter
    public File getSunyangLogo() {
    return sunyangLogo;
    }
    public void setSunyangLogo(File sunyangLogo) {
    this.sunyangLogo = sunyangLogo;
    }


    public String getSunyangLogoContentType() {
    return sunyangLogoContentType;
    }
    public void setSunyangLogoContentType(String sunyangLogoContentType) {
    this.sunyangLogoContentType = sunyangLogoContentType;
    }
    public String getSunyangLogoFileName() {
    return sunyangLogoFileName;
    }
    public void setSunyangLogoFileName(String sunyangLogoFileName) {
    this.sunyangLogoFileName = sunyangLogoFileName;
    }
    public String getSavePath() {
    HttpServletRequest request=ServletActionContext.getRequest();
    return request.getRealPath(savePath);
    }
    public void setSavePath(String savePath) {//取得文件上传的路径
    this.savePath = savePath;
    }

    public String execute()throws Exception{
    setSavePath("");
    FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getSunyangLogoFileName());
    FileInputStream fis=new FileInputStream(getSunyangLogoFileName());
    byte[] buffer=new byte[1024];
    int len=0;
    while((len=fis.read(buffer))>0){
    fos.write(buffer,0,len);
    }
    return SUCCESS;
    }
    }
    不知道是哪里空对象了啊?
      

  3.   

    InputStream fis=new FileInputStream(sunyangLogo);OutputStream fos=new FileOutputStream( new File(ServletActionContext.getRequest().getRealPath("保存的路径") , sunyangLogoFileName) );或者 试试这个:  OutputStream fos=new FileOutputStream( new File( savePath , sunyangLogoFileName)  );
      

  4.   

    这个我也奇怪的来着,我用FileInputStream fis=new FileInputStream(getSunyangLogo());时,编译都过不了,总是报The constructor FileInputStream(File) is undefined
    杳看APIFileInputStream
    public FileInputStream(File file)
                    throws FileNotFoundException通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。创建一个新 FileDescriptor 对象来表示此文件连接。 
    首先,如果有安全管理器,则用 file 参数表示的路径作为参数调用其 checkRead 方法。 如果指定文件不存在,或者它是一个目录,而不是一个常规文件,抑或因为其他某些原因而无法打开进行读取,则抛出 FileNotFoundException。难道是因为最后这句话:“如果指定文件不存在,或者它是一个目录,而不是一个常规文件,抑或因为其他某些原因而无法打开进行读取,则抛出 FileNotFoundException。”
      

  5.   

    在输入输出流前加了打印语句
    System.out.println(getSunyangLogoFileName());
    System.out.println(getSavePath());

    FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getSunyangLogoFileName());
    FileInputStream fis=new FileInputStream(getSunyangLogoFileName());可以看到控制台:2012-5-1 18:31:02 org.apache.catalina.startu
    信息: Server startup in 15840 ms
    null
    E:\study\JSP\Tomcat 6.0\webapps\fileupload
    说明取到了保存路径,但是没有取出文件名字,应该是没有取到上传的文件吧,其实我也纳闷,是怎么取到上传的文件的,虽说定义了这个方法,但是它是怎么就找到这个文件的呢?
    吃饭先~~饿,身体要紧
      

  6.   


    import org.apache.struts2.components.File;你导的是这个包????
    不是这个  ???import java.io.File;  ???
      

  7.   

    果真是也:这个我也奇怪的来着,我用FileInputStream fis=new FileInputStream(getSunyangLogo());时,编译都过不了,总是报The constructor FileInputStream(File) is undefined 
    上面这个问题解决了
    难道import org.apache.struts2.components.File;这里没有FileInputStream(File)这个方法?
    现在是依旧不能建输入流~~~~~~~~~~~~~
      

  8.   

    看下 关于文件上传的例子:
    http://blog.csdn.net/hzc543806053/article/details/7526306
      

  9.   

    private File sunyangLogo;//上会文件
     保证file中的name属性: <s:file name="sunyangLogo" ....../>
      

  10.   

    <!--  文件上传操作必须保证enctype="multipart/form-data"-->
     <s:form action="upload" enctype="multipart/form-data" method="post" namespace="/">
     <center><h3>上传图片</h3></center>
     <s:textfield name="title" label="图片名称" value=""/>
     <s:file name="sunyangLogo" label="上传路径"></s:file>
     <s:submit value="开始上传"/>
     </s:form>
    一定要有name="sunyangLogo"
      

  11.   

    用Debug短个点调式一下,一步一步的走,看是在哪一行代码出的错。
      

  12.   

    每次都是用System.out.println()去查看,看来真的要好好的习惯用调试
    结贴支~