<form name="addform" action="candiate/candiateadd.action" method="post" enctype ="multipart/form-data">
 <input id="zhuceImage" name="upfile"type="file"/>
 <input id="zhuceBimage" name="upfile" type="file"/>
 <input id="zhuceName" name="candiate.candiateName" type="text"/>
</formAction:public class CandiateAction {
private Candiate candiate;
         private List<File> upfile;
private List<String> fileFileName;
private List<String> fileContentType; 
         get  set方法
public String candiateadd() throws IOException{
//得到工程保存图片的路径
        String address = ServletActionContext.getServletContext().getRealPath("/images/upload");
        String relateAddress = "images/upload";//保存到数据库的相对路径
        System.out.println(address);
        System.out.println(upfile.size());
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateTime = sdf.format(new Date());
        //循环上传的文件
        for(int i = 0 ; i < upfile.size() ; i ++){
            InputStream is = new FileInputStream(upfile.get(i));            //得到图片保存的位置(根据address来得到图片保存的路径在tomcat下的该工程里)
            File destFile = new File(address,this.getFileFileName().get(i)+dateTime);
            this.getFileFileName().get(i)取不到上传文件的名字
            //把图片写入到上面设置的路径里
            OutputStream os = new FileOutputStream(destFile);
            byte[] buffer = new byte[400];
            int length  = 0 ;
            while((length = is.read(buffer))>0){
                os.write(buffer, 0, length);
            }
            is.close();
            os.close();
        }
        //将上传的文件路径存入数据库
        candiate.setCandiateImage(relateAddress+"/"+this.getFileFileName().get(0)+dateTime);
        candiate.setCandiateBimage(relateAddress+"/"+this.getFileFileName().get(1)+dateTime);
return "index";
}
}struts.xml:<package name="candiateadd" namespace="/candiate" extends="struts-default">
         <action name="candiateadd" class="com.tyut.actions.CandiateAction" method="candiateadd">
             <interceptor-ref name="candiateadd">
                  <param name="allowedTypes">
                       image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png
                  </param>
             </interceptor-ref>
             <interceptor-ref name="defaultStack"/>
             <result name="success">/index.jsp</result>
         </action>
    </package>web.xml:<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>以上是我根据网上搜到的自己写的。运行出现错误,不知道为什么,希望大家能出来解释下,我是新手,希望能够解释详细点,
分立马送上。
错误的提示是这样的:
错误1:
严重: Exception starting filter struts2
Unable to load configuration. - interceptor-ref - file:/D:/Program%20Files/NetBeans%206.7.1/Apache%20Software%20Foundation/Apache%20Tomcat%206.0.18/webapps/zxtp/WEB-INF/classes/struts.xml:91:50
Caused by: Unable to load configuration. - interceptor-ref - file:/D:/Program%20Files/NetBeans%206.7.1/Apache%20Software%20Foundation/Apache%20Tomcat%206.0.18/webapps/zxtp/WEB-INF/classes/struts.xml:91:50
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
... 29 more
Caused by: Unable to find interceptor class referenced by ref-name candiateadd - interceptor-ref - file:/D:/Program%20Files/NetBeans%206.7.1/Apache%20Software%20Foundation/Apache%20Tomcat%206.0.18/webapps/zxtp/WEB-INF/classes/struts.xml:91:50
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:52)
错误2:是我把struts2.xml里的<interceptor-ref name="candiateadd">
                  <param name="allowedTypes">
                       image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png
                  </param>
             </interceptor-ref>
             <interceptor-ref name="defaultStack"/>
去掉后运行项目提示
java.lang.NullPointerException
com.tyut.actions.CandiateAction.candiateadd(CandiateAction.java:100)
          more...
该怎么解决,我是新手,希望能解释的详细点,解决后分马上送上

解决方案 »

  1.   

    参考: http://blog.csdn.net/voyage_mh1987/article/details/5884966
    错误的原因:
    1: 没有转码
    2: 拦截器配置错误
    3: 名称没有按照规范命名
    4: 没哟设置流格式
    5: struct版本不一直导致无法加载配置文件
    6: action 配置错误或者没有对应上<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    <!-- developer mode convenient for Debug --> <constant name="struts.i18n.encoding" value="GBK" />
    <constant name="struts.ui.theme" value="simple" />
    <constant name="struts.ui.templateDir" value="template" />
    <constant name="struts.ui.templateSuffix" value="ftl" />
    <package name="uploadFile" extends="struts-default"> <action name="myUpload" class="com.UploadAction">
    <interceptor-ref name="fileUpload">
    <param name="savePath">/file</param>
    <param name="maximumSize">1024000</param>
    </interceptor-ref>
    <interceptor-ref name="defaultStack" />
    <result name="input">/index.jsp</result>
    <result name="success">/showUpload.jsp</result>
    </action>
    <action name="download" class="com.DownLoadAction">
    <!-- 设置文件名参数,由页面上传入 -->
    <param name="fileName"></param>
    <result name="success" type="stream">
    <!-- 下载文件类型定义 -->
    <param name="contentType">
    application/octet-stream
    </param>
    <!-- 下载文件处理方法 -->
    <param name="contentDisposition">
    attachment;filename="${downloadChineseFileName}"
    </param>
    <!-- 下载文件输出流定义 -->
    <param name="inputName">downloadFile</param>
    <param name="bufferSize">4096</param>
    </result>
    </action>
    </package>
    </struts>
      

  2.   

    楼上的网址我已经看了,我改成你的那样,还是报
    java.lang.NullPointerException
    com.tyut.actions.CandiateAction.candiateadd(CandiateAction.java:100)
      more...
    麻烦你帮忙看下我写的代码,到底哪个地方出错了?
    struts.xml:先不管上传的是什么文件,不设置拦截器<package name="candiateAdd" namespace="/candiate" extends="struts-default">
             <action name="candiateAdd" class="com.tyut.actions.CandiateAction" method="candiateAdd">
                 <result name="success">/index.jsp</result>
             </action>
        </package>public class CandiateAction {
    private Candiate candiate; private File[] uploadFile;//上传的文件名   
              private String[] contentType;  
    private String[] caption;  
    private String[] uploadFilename;  
    所有的get  set方法。。
    /** 
      * 拷贝文件到相应的目录下 
      * @param src 
      * @param dst 
      */  
    private static void copy(File src, File dst) {  
            try {  
               InputStream in = null;  
               OutputStream out = null;  
               try {  
                   in = new BufferedInputStream(new FileInputStream(src));  
                   out = new BufferedOutputStream(new FileOutputStream(dst));  
                    byte[] buffer = new byte[16 * 1024];  
                    while (in.read(buffer) > 0) {  
                        out.write(buffer);  
                    }  
               } finally {  
                    if (null != in) {  
                        in.close();  
                    }  
                    if (null != out) {  
                       out.close();  
                   }  
               }  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
    }  
           public String candiateAdd() throws IOException{
    //得到工程保存图片的路径
            String address = ServletActionContext.getServletContext().getRealPath    ("/images/upload");
            String relateAddress = "images/upload";//保存到数据库的相对路径
            System.out.println(address);
            System.out.println(uploadFile.length);
            //上面的address和uploadFile.length都能输出值。        //上传的image文件,对应uploadFile的第一个图片
             File imageFile = null;
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateTime = sdf.format(new Date());
            String candiateImageName = dateTime+new String(uploadFilename[0]);
            imageFile = new File(address+"/"+candiateImageName);
            
            copy(this.getUploadFile()[0],imageFile);
            
            candiate.setCandiateImage(relateAddress+"/"+candiateImageName);
            
            //上传bimage文件对应第二个图片,本项目只需要上传两个图片
             File bimageFile = null;
            
            String dateTime1 = sdf.format(new Date());
            String candiatebimageName = dateTime1+new String(uploadFilename[1]);
            
            bimageFile = new File(address+"/"+candiatebimageName);
            copy(this.getUploadFile()[1],bimageFile);
            
            candiate.setCandiateBimage(relateAddress+"/"+candiatebimageName);
            
            System.out.println(candiate.getCandiateImage());
            
    return "success";
    }
    }jsp页面:<form name="addform" action="candiate/candiateAdd.action" method="post" enctype ="multipart/form-data">
     <input id="zhuceImage" name="upfile" type="file"/>
     <input id="zhuceBimage" name="upfile" type="file"/>
     <input id="zhuceName" name="candiate.candiateName" type="text"/>
    </form项目的jar包:
    antlr-2.7.6.jar
    asm-attrs.jar
    asm.jar
    cglib-2.1.3.jar
    commons-beanutils.jar
    commons-collections-2.1.1.jar
    commons-collections.jar
    commons-fileupload-1.2.2.jar
    commons-io-2.0.1.jar
    commons-lang.jar
    commons-logging-1.0.4.jar
    commons-logging.jar
    dom4j-1.6.1.jar
    ehcache-1.2.3.jar
    ejb3-persistence.jar
    ezmorph-1.0.6.jar
    freeer-2.3.15.jar
    hibernate-annotations.jar
    hibernate-commons-annotations.jar
    hibernate-entitymanager.jar
    hibernate-validator.jar
    hibernate3.jar
    jaas.jar
    javassist.jar
    jaxen-1.1-beta-7.jar
    jboss-archive-browsing.jar
    jdbc2_0-stdext.jar
    json-lib-2.2.3-jdk13.jar
    jsonplugin-0.34.jar
    jstl.jar
    jta.jar
    log4j-1.2.11.jar
    msbase.jar
    mssqlserver.jar
    msutil.jar
    ognl-2.7.3.jar
    standard.jar
    struts2-core-2.1.8.jar
    xml-apis.jar
    xwork-2.1.2.jar
    xwork-core-2.1.6.jar
      

  3.   

    楼主,你好
    现在,专心下来看代码的人少了,而且对于新手,还有很多都不知道其中的原理,我也是最近开始学struts2的。
    <input id="zhuceImage" name="upfile" type="file"/>
    <input id="zhuceBimage" name="upfile" type="file"/>
    注意这里的file的name值,你设的为upfile
    那么Action里的绑定的upfile---》对应文件
    upfileFileName---》对应名字
    把这几个字段该了就行了
    特别注意struts2是怎么绑定。
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String dateTime = sdf.format(new Date());
    String candiateImageName = dateTime+upfileFilename[0];
    楼主记得给分哈
      

  4.   

    关于报NULL错误
    你jsp页面写的是这样的
    <input id="zhuceImage" name="upfile" type="file"/>
    <input id="zhuceBimage" name="upfile" type="file"/>
    Action里
    private List<File> upfile;  ---》估计楼主恰好设对了
    private List<String> fileFileName;  ---》错,应该为upfileFileName
    private List<String> fileContentType; ---》错,应该为upfileContentType
    this.getFileFileName().get(i)当然报NULL错误了应该为this.getUpfileFileName.get(i)