webwork的文件上传总是得不到文件对象

xml

<interceptor name="fileupload" class="com.opensymphony.webwork.interceptor.FileUploadInterceptor"/>
            
            <interceptor-stack name="uploadStack">
                <interceptor-ref name="fileupload"/>
                <interceptor-ref name="defaultStack"/>
             </interceptor-stack>
<package name="fileUpload" namespace="/fileUpload" extends="webwork-default">
        <default-interceptor-ref name="uploadStack" />
        <action name="fileUpload" class="com.casetrial.fileUpload.UploadAction">
            <result name="success">/pages/appealSituation/fileUploadIndex.jsp</result>
        </action>
    </package>

 action

public class UploadAction extends ActionSupport {
    private File[] files;             //多个文件对象数组
    private String[] filesFileName;   //文件对应的真实文件名  单个文件上传时不需要此属性定义 
                                      //多文件上传时 file.getName() 无效,取得不是真实的文件名,而是upload_xxx.tmp
    public String execute(){
        System.out.println(files==null);
        if (files != null){
            for (int i = 0; i < files.length; i++){
                File file = files[i];
                String fileName = filesFileName[i];
                System.out.print(" File list: ");
                System.out.print("   name:=" + file.getName() + " ");
                System.out.print("   fileName:=" + fileName + " ");
                System.out.print("   path:=" + file.getPath() + " ");
                System.out.print("  ");

                FileOutputStream outputStream = null;
                FileInputStream fileIn = null;
                ResourceBundle rb = ResourceBundle.getBundle("config");
                String fileDir = rb.getString("saveDir")+File.separator;
                String filePath = fileDir+fileName;
                
                File f = new File(fileDir);
                f.mkdirs();
                try {
                    outputStream = new FileOutputStream(filePath);
                    fileIn = new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                byte[] buffer = new byte[1024];
                int len;
                try {
                    while((len=fileIn.read(buffer))>0){
                        outputStream.write(buffer,0,len);
                    }
                    fileIn.close();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("uploadfile name="+fileName);
                
            }
        }else{
            System.out.println("file is null!");
        }
        return SUCCESS;
    }

    public String[] getFilesFileName(){
        return filesFileName;
    }

    public void setFilesFileName(String[] filesFileName){
        this.filesFileName = filesFileName;
    }

    public File[] getFiles(){
        return files;
    }

    public void setFiles(File[] files){
        this.files = files;
    }

    /**//**
     * 获取session对象
     * @return
     */
    public HttpSession getSession(){
        return getRequest().getSession();
    }

    /**//**
     * 获取request对象
     *
     * @return httpServletRequest
     */
    public HttpServletRequest getRequest(){
        return ServletActionContext.getRequest();
    }

    /**//**
     * 获取response对象
     *
     * @return HttpServletResponse
     */
    public HttpServletResponse getResponse(){
        return ServletActionContext.getResponse();
    }

    /**//**
     * 获取ServletContext对象
     *
     * @return ServletContext
     */
    public ServletContext getContext(){
        return ServletActionContext.getServletContext();
    }

    /**//**
     * 获取ActionContext
     *
     * @return
     */
    public ActionContext getActionContext(){
        return ActionContext.getContext();
    }

    /**//**
     * 获取参数map
     *
     * @return
     */
    public Map getParameterMap(){
        return getActionContext().getSession();
    }

    /**//**
     * 获取输入参数名字对应的值
     *
     * @param name
     * @return
     */
    public String getParameter(String name){
        return (String) getParameterMap().get(name);
    }
}

 

页面

<html>
    <head>
    </head>
    <script language="Javascript" charset="utf-8">
    function addAccessory(){
        var accessoryListObj = document.getElementById("accessoryList");
        var accObj = document.createElement("span");
        var spanId = (new Date()).getTime();
        var spanHTML = " <input name='files' type='file' /><input type='button' value='删除' onclick=deleteFile('" + spanId + "')><br>";

        accObj.setAttribute("id", spanId);
        accObj.setAttribute("width", "100px");
        accObj.innerHTML = spanHTML;
        accessoryListObj.appendChild(accObj);
    }

    function deleteFile(fileId){
        var accessoryListObj = document.getElementById("accessoryList");
        var accObj = document.getElementById(fileId);
        accessoryListObj.removeChild(accObj);
    }
</script>
    <body>
        <form action="../fileUpload/fileUpload.action"
            enctype="multipart/form-data" method="post">
            <table id="DataTable">
                <tbody>
                    <tr>
                        <td class="style2" colspan="4">
                            <strong>附件: </strong>
                            <span title="增加附件" onclick="addAccessory()"
                                style="cursor:pointer;color:blue;"><strong>[添加附件]</strong>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" style="width:100%;height:300px;">
                            <div id="accessoryList"
                                style="overflow-x:auto;overflow-y:auto;width:1000px;height:100%;">
                                &nbsp;
                                <input type="file" name="files"> 
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" style="width:100%;height:15px;" align="center">
                            <input type="Submit" value="提交">

                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
</html>

 

一运行总是会发现files对象为Null,就得不到所要上传的东西,大家看看是错在哪儿了,我看了半天找不出来

[code="xml"]

        <interceptor-stack name="uploadStack">   
            <interceptor-ref name="fileupload"/>   
            <interceptor-ref name="defaultStack"/>   
         </interceptor-stack>  [/code]

[color=blue]
[b]defaultStack中,fileupload这个Interceptor有。而你的配置,打乱了Interceptor的顺序,所以得不到值了。

直接使用默认的defaultStack就可以了。改一下试试吧。[/b][/color]

[color=blue][b]你的Interceptor配置的不对。

直接使用defaultStack就可以了。
[/b][/color]

你去看看struts-core-***.lib这个包中的struts-default.xml默认配置。之后你就明白了怎么配置了。

[b]defaultStack拦截器堆栈。[/b]
[code="xml"]














dojo..*



input,back,cancel,browse


input,back,cancel,browse

[/code]

[b]fileUploadStack拦截器堆栈[/b]
[code="xml"]

[/code]

这两个拦截器堆栈都有fileUpload拦截器。
你这里是小写啊。
[b]如果是简单的文件上传就使用fileUploadStack就行啦。[/b]

<!ELEMENT package (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, default-class-ref?, global-results?, global-exception-mappings?, action*)>

interceptor位置不正确